#! /usr/bin/perl 

use 5.8.0;
use strict;
use Switch;
use Getopt::Std;
use MIME::Lite;

#===============================================================
# This module provides filters to processing Emdeon CSV files:
#	    1. Commercial medicaid
#		2. Tpl
#		3. Medicare
#
# Usage:
#      convert_emdeon.pl -b rdate inCSVfile  outCSVfile
#
#===============================================================

package main;

our $bad_record_num = 0;
our $err_monitor;

#----------------------------------------------------------------
# This array is used to control the output order of each record 
# in all the following three read() functions.
#
# It is used in every process() following.
#----------------------------------------------------------------
our @fdname = qw(
		cust_id
		hid
		our_run_date
		file_date
		emdeon_matchkey  
		acctnum
		service_date_begin
		service_date_end
		charges
		balance

		pat_proc_type
		pat_type
		obs_ind
		output_id
		active_flag
		suppress_flag
		processing_ssn_flag
		quality
                search_pattern
		emdeon_ins_type
                medlytix_ins_type
		flag_mismatch_lname
		flag_mismatch_fname
		flag_mismatch_ssn
		flag_mismatch_dob

                name_type     
                lname        
                mname      
                fname    
                addr1   
                city                     
                state                  
                ssn                
                dob              
                gender                   

                patient_lname           
                patient_mname           
                patient_fname          
                patient_addr1         
                patient_addr2        
                patient_city        
                patient_state     
                patient_zip     
                patient_ssn    
                patient_dob   
                patient_gender    

		subscriber_lname  
		subscriber_mname
		subscriber_fname  
		subscriber_addr1
		subscriber_addr2  
		subscriber_city
		subscriber_state
		subscriber_zip
		subscriber_ssn  
		subscriber_dob    
		subscriber_id  

		carrier_code
		insurance_desc
		insurance_contact_name
		insurance_contact_phone
		insurance_addr 
		insurance_city 
		insurance_state 
		insurance_zip
		group_number
		plan_type 
		plan_number
		network_id
		effective_date 
		effective_date_end

		c_insurance_desc1
		c_subscriber_id1
		c_effective_date1
		c_effective_date_end1
		c_contact_phone1
		c_insurance_desc2
		c_subscriber_id2
		c_effective_date2
		c_effective_date_end2
		c_contact_phone2

		insurance_desc2
		subscriber_id2
		effective_date2
		effective_date_end2
		insurance_contact_phone2
		insurance_desc3
		subscriber_id3
		effective_date3
		effective_date_end3
		insurance_contact_phone3

		client_field1
		client_field2
		client_field3
		client_date1
);
	

#============================================================
# Now it is in commercial MEDICAID package
#============================================================

package COM_MEDICAID;

#---------------------------------------------------
# This hash defines the name of each carrier_code.
#-------------------------------------------------
our %insurance_desc = ( 
 '00006' => 'GEORGIA MEDICAID / PEACHCARE',
 '00007' => 'WYOMING MEDICAID',
 '00009' => 'WASHINGTON DC MEDICAID',
 '00013' => 'WISCONSIN MEDICAID',
 '00014' => 'KENTUCKY MEDICAID',
 '00015' => 'ARKANSAS MEDICAID',
 '00016' => 'CALIFORNIA ST MEDICAID',
 '00017' => 'TENNESSEE MEDICAID / TENNCARE',
 '00019' => 'NORTH CAROLINA MEDICAID',
 '00021' => 'MINNESOTA MEDICAID',
 '00022' => 'KANSAS MEDICAID',
 '00023' => 'NEW YORK ST MEDICAID',
 '00024' => 'MEDICARE',
 '00025' => 'WASHINGTON ST MEDICAID',
 '00027' => 'WEST VIRGINIA MEDICAID',
 '00029' => 'UNITED HEALTHCARE',
 '00032' => 'ILLINOIS MEDICAID',
 '00033' => 'TRICARE',
 '00035' => 'NORTH DAKOTA MEDICAID',
 '00036' => 'FLORIDA MEDICAID',
 '00037' => 'OKLAHOMA MEDICAID',
 '00038' => 'ALABAMA MEDICAID',
 '00039' => 'PENNSYLVANIA MEDICAID',
 '00040' => 'MISSISSIPPI MEDICAID',
 '00041' => 'NEW MEXICO MEDICAID',
 '00042' => 'CIGNA',
 '00043' => 'IOWA MEDICAID',
 '00045' => 'INDIANA MEDICAID',
 '00046' => 'NEW JERSEY MEDICAID',
 '00048' => 'LOUISIANA MEDICAID',
 '00049' => 'VIRGINIA MEDICAID / VMAP FHSC VA',
 '00054' => 'OREGON MEDICAID',
 '00055' => 'PUERTO RICO MEDICAID',
 '00056' => 'ARIZONA MEDICAID',
 '00066' => 'TEXAS MEDICAID / HEALTHCARE',
 '00076' => 'MISSOURI MEDICAID',
 '00081' => 'SOUTH CAROLINA MEDICAID',
 '00085' => 'COLORADO MEDICAID',
 '00086' => 'OHIO MEDICAID',
 '00090' => 'MICHIGAN MEDICAID',
 '00092' => 'AETNA',
 '00099' => 'HUMANA',
 '00113' => 'HAWAII MEDICAID',
 '00116' => 'NEVADA MEDICAID',
 '00123' => 'BLUE CROSS / BLUE SHIELD',
 '00148' => 'MAINE MEDICAID',
 '00156' => 'CONNECTICUT MEDICAID',
 '00175' => 'IDAHO MEDICAID',
 '00178' => 'MASSACHUSETTS MEDICAID',
 '00196' => 'MEDICA / UNITED HEALTHCARE',
 '00240' => 'MARYLAND MEDICAID',
);


#-------------------------------------------------
# Commercial carrier codes
#-------------------------------------------------
our %com_ccode = (
		'00029' => 1, 
		'00033' => 1, 
		'00042' => 1, 
		'00092' => 1,
		'00099' => 1, 
		'00123' => 1, 
		'00196' => 1,
		);

#--------------------------------------------------------
# Start index of the repeated data chunks in each record
#--------------------------------------------------------
#our @chunks = (86,145,204,263,322,381,440,499,558,617); 
#our @chunks = (93,152,211,270,329,388,447,506,565,624);
our @chunks = (108,167,226,285,344,403,462,521,580,639);


#------------------------------------------------------------
# process(record, outfile, file_date, our_run_date)
#
#   Processing Emdeon commercial medicaid data file, and 
# saving the result in the output file.
#-----------------------------------------------------------
sub process($$$$$) 
{
	my $line = shift;
	my $OUTFH = shift;
	my $file_date = shift;
	my $our_run_date = shift;
	my $ctid = shift;

	$line =~ s/[^\x20-\x7e]//g;
	my @fields = split(/;/, $line);
	return if $fields[0] !~ /Remediated/i;			# record type
	
	my %h=();
	
	$h{'carrier_code'}            = $fields[1];
	$h{'input_ssn'}     	      = $fields[3];
	$h{'input_lname'}             = $fields[4];
	$h{'input_fname'}             = $fields[5];
	$h{'input_mname'}             = $fields[6];
	$h{'emdeon_matchkey'}         = $fields[17];
	$h{'cust_id'}                 = $ctid;
        $h{'our_run_date'}            = $our_run_date;
        $h{'file_date'}               = $file_date;


        ##################################################################################################

        # SUBSCRIBER NAME, DOB and SUBSCRIBER ID;
	$h{'subscriber_dob'} = ::date10($fields[65]);	
	$h{'subscriber_id'}    = $fields[61];	# except '00017'
	$h{'subscriber_lname'} = $fields[62];
        $h{'subscriber_fname'} = $fields[63];
	$h{'subscriber_mname'} = $fields[64];

        # SUBSCRIBER ADDRESS INFO
	$h{'subscriber_addr1'} = $fields[67];
	$h{'subscriber_addr2'} = $fields[69];
	$h{'subscriber_city'}  = $fields[71];
	$h{'subscriber_state'} = $fields[72];
	$h{'subscriber_zip'}   = $fields[73];


        #################################################################################################
        # NOTE THAT BLOCKS OF DATA CONTAINING PATIENT NAME AND DOB INFORMATION ARE IN TWO DIFFERENT PLACES


        $h{'patient_lname'}          = $fields[47];
        $h{'patient_fname'}          = $fields[48];
        $h{'patient_dob'}   = ::date10($fields[49]);

        # WE OVERWRITTE THE ORIGINAL VALUES IF THE FIELDS BELOW ARE POPULATED
        if ( $fields[93] ne "") { $h{'patient_lname'} = $fields[93];}
        if ( $fields[94] ne "") { $h{'patient_fname'} = $fields[94];}
        if ( $fields[95] ne "") { $h{'patient_mname'} = $fields[95];}
        if ( $fields[96] ne "") { $h{'patient_dob'} = ::date10($fields[96]);}
        if ( $fields[97] ne "") { $h{'patient_gender'} = $fields[97];}

        # FOR GENDER: IF NOT POPULATED AND SUBSCRIBER = PATIENT THEN POPULATE WITH SUBSCRIBER FIELD
        if ( ($h{'patient_gender'} eq "")                    && 
             ($fields[96] eq $fields[65])                    && 
             ($h{'patient_lname'} eq $h{'subscriber_lname'}) &&
             ($h{'patient_fname'} eq $h{'subscriber_fname'})
           ) 
           { $h{'patient_gender'} = $fields[66] ;}


        # PATIENT ADDRESS INFO
        $h{'patient_addr1'} = $fields[98];
        $h{'patient_addr2'} = $fields[100];
        $h{'patient_city'}  = $fields[102];
        $h{'patient_state'} = $fields[103];
        $h{'patient_zip'}   = $fields[104];



        ##################################################################################################

        # TRUNCATE MIDDLE NAMES AND GENDER TO ONE BYTE
	$h{'input_mname'} =~ s/^\s*(\w).*$/$1/;
	$h{'subscriber_mname'} =~ s/^\s*(\w).*$/$1/;
        $h{'patient_mname'} =~ s/^\s*(\w).*$/$1/;
        $h{'patient_gender'} =~ s/^\s*(\w).*$/$1/;



        ##################################################################################################

        # CAPTURE THE search_pattern FIELD

        if ( $fields[699] =~ /SearchStats/i ) {$h{'search_pattern'} = uc($fields[700]) ;}
        else                                  {$h{'search_pattern'} = "";}
        $h{'search_pattern'} = substr($h{'search_pattern'}, 0, 30) if length($h{'search_pattern'}) > 30;


        ##################################################################################################

        # INITIALIZATION OF FLAGS

	$h{'suppress_flag'}    = 0;
	$h{'active_flag'}      = -1 ;	# initial value 

        # initialize to value D = Medicaid OR C = Commercial;
        $h{'medlytix_ins_type'} = ($com_ccode{$h{'carrier_code'}})? "C" : "D";

	if ( $fields[22] !~ /^\s*$/) {	#err msg
		#-- $fields[20] is the NPI
		::watch_error($fields[20], $h{'carrier_code'}, $fields[22]);
		return;
	}


        ###################################################################################################
        # CALL THE HANDLERS

	my $no_handler = 0;
	switch ( $h{'carrier_code'} ) {
		case '00006' { hdl_00006(\%h, \@fields); }
		case '00029' { hdl_00029(\%h, \@fields); }
		case '00033' { hdl_00033(\%h, \@fields); }
		case '00042' { hdl_00042(\%h, \@fields); }
		case '00092' { hdl_00092(\%h, \@fields); }
		case '00099' { hdl_00099(\%h, \@fields); }
		case '00123' { hdl_00123(\%h, \@fields); }
		case '00196' { hdl_00196(\%h, \@fields); }
		case '00009' { hdl_00009(\%h, \@fields); }
		case '00014' { hdl_00014(\%h, \@fields); }
		case '00015' { hdl_00015(\%h, \@fields); }
		case '00016' { hdl_00016(\%h, \@fields); }
		case '00017' { hdl_00017(\%h, \@fields); }
		case '00019' { hdl_00019(\%h, \@fields); }
		case '00023' { hdl_00023(\%h, \@fields); }
		case '00025' { hdl_00025(\%h, \@fields); }
		case '00027' { hdl_00027(\%h, \@fields); }
		case '00032' { hdl_00032(\%h, \@fields); }
		case '00036' { hdl_00036(\%h, \@fields); }
		case '00037' { hdl_00037(\%h, \@fields); }
		case '00038' { hdl_00038(\%h, \@fields); }
		case '00039' { hdl_00039(\%h, \@fields); }
		case '00040' { hdl_00040(\%h, \@fields); }
		case '00041' { hdl_00041(\%h, \@fields); }
		case '00045' { hdl_00045(\%h, \@fields); }
		case '00046' { hdl_00046(\%h, \@fields); }
		case '00048' { hdl_00048(\%h, \@fields); }
		case '00049' { hdl_00049(\%h, \@fields); }
		case '00054' { hdl_00054(\%h, \@fields); }
		case '00056' { hdl_00056(\%h, \@fields); }
		case '00066' { hdl_00066(\%h, \@fields); }
		case '00076' { hdl_00076(\%h, \@fields); }
		case '00081' { hdl_00081(\%h, \@fields); }
                case '00085' { hdl_00085(\%h, \@fields); }
		case '00086' { hdl_00086(\%h, \@fields); }
		case '00090' { hdl_00090(\%h, \@fields); }
		case '00116' { hdl_00116(\%h, \@fields); }
		case '00156' { hdl_00156(\%h, \@fields); }
                case '00175' { hdl_00175(\%h, \@fields); }
		case '00178' { hdl_00178(\%h, \@fields); }
		case '00240' { hdl_00240(\%h, \@fields); }
		else { $no_handler =  1;}
	}
	return if $no_handler == 1;
	;#print "$line\n" if $h{'active_flag'} <= 0;	


        ####################################################################################
        # EXCLUDE IF NOT FOUND ACTIVE OR INACTIVE
        # EXCLUDE IF NON-MEDICAL

	return if $h{'active_flag'} < 0;	
	return if $h{'plan_type'} =~ /(VISION|VIEW|DENTAL|DNTL|PHARM|RX|Chiropractic)/i; 


        ####################################################################################
        # TREATMENT OF PLAN TYPE AND TRUNCATION
        $h{'plan_type'} =~ s/\s*\-\s*\-\s*$/ -  / if $h{'plan_type'} =~ /\-\s*\-/;
	$h{'plan_type'} =~ s/^\s*\-\s*// if $h{'plan_type'} =~ /^\s*\-/; 
        $h{'plan_type'} =~ s/\s*\-\s*$// if $h{'plan_type'} =~ /\-\s*$/;
	$h{'plan_type'} = substr($h{'plan_type'}, 0, 118) if length($h{'plan_type'}) >119; 


        ####################################################################################
        # TREATMENT OF SSN
        # FOR SOCIAL: IF SUBSCRIBER = PATIENT THEN POPULATE subscriber_ssn WITH patient_ssn

        if (! ::ssn_ok($h{'patient_ssn'}) ) {
                $h{'patient_ssn'} = '';
        }

        # extract specially from Medicare subscriber ID
        if ( ( $h{'patient_ssn'} eq "") && ($h{'input_ssn'} eq substr($h{'subscriber_id'}, 0, 9) ) ) {

                 $h{'patient_ssn'} = substr($h{'subscriber_id'}, 0, 9);
        }

        if ( 
             ($fields[96] eq $fields[65])                    &&
             ($h{'patient_lname'} eq $h{'subscriber_lname'}) &&
             ($h{'patient_fname'} eq $h{'subscriber_fname'})
           )
           { $h{'subscriber_ssn'} = $h{'patient_ssn'} ;}


        ####################################################################################
        # ASSIGNMENT OF insurance_desc NAME BASED ON HASH AND emdeon_ins_type BASED ON HASH

	$h{'insurance_desc'} = $insurance_desc{$h{'carrier_code'}};
        $h{'emdeon_ins_type'} = ($com_ccode{$h{'carrier_code'}})? "C" : "D";


        ####################################################################################
        # SUPPRESSION OF SOME ACCOUNTS (THESE WILL BE LOADED BUT NOT RETURNED TO CLIENT
        # FIRST WHEN NOT ACTIVE
        # SECOND SUPPRESS CERTAIN MEDICAID PLANS

	$h{'suppress_flag'} +=1 if $h{'active_flag'} == 0;

        # below apply custom and global suppression for specific Medicaid Plans
	if (
		( ($h{'carrier_code'} eq '00046') && ($h{'plan_type'} =~ /SVC\sPKG\sG/i)) 
		or 
                ( ($h{'carrier_code'} eq '00045') && ($h{'plan_type'} =~ /PACKAGE\sB\sPREGNANCY\sCOVERAGE/i))
                or
		( ($h{'carrier_code'} eq '00037') && ($h{'plan_type'} =~ /(MENTAL\sHEALTH|FAMILY\sPLANNING)/i) )
		or
		( ($h{'carrier_code'} eq '00039') && $h{'plan_type'} =~ /(MHX-00-EPOMS|PSF-00-HCB15|TJ-22-HCB13)/i)
                or
                ( ($h{'carrier_code'} eq '00081') && $h{'plan_type'} =~ /(PCAT:\s*48|PCAT:\s*52|PCAT:\s*90|PCAT:\s*55)/i)
                or
                ( ($h{'carrier_code'} eq '00015') && $h{'plan_type'} =~ /(53NONMED|55NONMED|69FAM)/i)
                or
                ( ($h{'carrier_code'} eq '00036') && $h{'plan_type'} =~ /(EBA|FP-)/i)
                or
                ( ($h{'carrier_code'} eq '00038') && $h{'plan_type'} =~ /CAT=50\sPLAN\sFIRST/i)
                or
                ( ($h{'carrier_code'} eq '00025') && $h{'plan_type'} =~ /(FPSO|TCFPO)/i)
		or
                ( ($h{'carrier_code'} eq '00032') && $h{'plan_type'} =~ /DHS SOCIAL SERVICES/i)
                or
		( ($h{'carrier_code'} eq '00019') && $h{'plan_type'} =~ /(MAFD|MPWN)/i)
	) {
		$h{'suppress_flag'} +=1000;
	}

	#------------------------------------------
	# Normalize date format to mm/dd/yyyy
	#------------------------------------------
	$h{'effective_date'}		= ::date10($h{'effective_date'});
	$h{'effective_date_end'}	= ::date10($h{'effective_date_end'});
	$h{'effective_date2'}		= ::date10($h{'effective_date2'});
	$h{'effective_date_end2'}	= ::date10($h{'effective_date_end2'});
	$h{'effective_date3'}		= ::date10($h{'effective_date3'});
	$h{'effective_date_end3'}	= ::date10($h{'effective_date_end3'});
	$h{'c_effective_date1'}		= ::date10($h{'c_effective_date1'});
	$h{'c_effective_date_end1'}	= ::date10($h{'c_effective_date_end1'});
	$h{'c_effective_date2'}		= ::date10($h{'c_effective_date2'});
	$h{'c_effective_date_end2'}	= ::date10($h{'c_effective_date_end2'});

        $h{'insurance_desc2'} = "MEDICARE A" if $h{'insurance_desc2'} =~ /Mcare A/i;
        $h{'insurance_desc2'} = "MEDICARE B" if $h{'insurance_desc2'} =~ /Mcare B/i;
        $h{'insurance_desc3'} = "MEDICARE A" if $h{'insurance_desc3'} =~ /Mcare A/i;
        $h{'insurance_desc3'} = "MEDICARE B" if $h{'insurance_desc3'} =~ /Mcare B/i;


		#----------------
		# @fdname determines the order of output
		#----------------
	::print_record(\%h, \@::fdname, $OUTFH);
}



#-----------------------------------------------------------------
# Handler for "GA Medicaid" data processing.
#
# The record status can be determined by the first chunk of
# data.  If field[86] is 'Inactv', the record is inactive;
# if field[86] is 'Actv Cvg', field[89] is 'Health Bene Plan Cvg',
# then record is active.
#
# For some record, both field[86] and field[145] are 'Actv Cvg', 
# the status is still determined by the first chunk.
#
# Multiple chunks are searched for more insurance information.
#-----------------------------------------------------------------
sub hdl_00006 {
	my $href = shift;
	my $flds = shift;

	foreach my $i (@chunks) { 
		last if length($flds->[$i]) == 0;

		if( $i == 108 ){	# record status in first chunk
			if( $flds->[$i] =~ /Inactv/i ) { 
				$href->{'active_flag'} = 0 ;		
			}
			elsif( $flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i && 
				$flds->[$i] =~ /Actv\s+Cvg/i )
			{
				$href->{'active_flag'} = 1;
				$href->{'effective_date'}     = '';
				$href->{'effective_date_end'} = '';
				$href->{'plan_type'}= $flds->[8+$i];
				if( $flds->[5+$i] =~ /^Mcaid/i ) {		  
					$href->{'plan_type'}= "MEDICAID - " . $flds->[8+$i];
				} 
			} 
		}

		elsif ( $flds->[$i] =~ /PCP/i && 
		        $flds->[8+$i] =~ /GEORGIA FAMILIES/i &&
		        $flds->[38+$i] =~ /Prov/i )
		{
			if ( ! exists $href->{'c_insurance_desc1'} ) {
				$href->{'c_insurance_desc1'} = "$flds->[39+$i]";
				$href->{'c_effective_date1'}     = '';
				$href->{'c_effective_date_end1'} = '';

				if( $flds->[57+$i] =~ /Phone/i ) {
					$href->{'c_contact_phone1'} = $flds->[58+$i];
				}
			}
		}

		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[38+$i] =~ /Pay.r/i &&
				$flds->[3+$i] !~ /(VISION|VIEW|DENTAL|DNTL|PHARM|RX|Chiropractic)/i )
		{
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[39+$i];
				$href->{'effective_date2'} = '';
				$href->{'effective_date_end2'} = '';
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( (! exists $href->{'insurance_desc3'}) && ( $flds->[39+$i] ne $href->{'insurance_desc2'}) ) {
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				$href->{'effective_date3'} = '';
				$href->{'effective_date_end3'} = '';
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
		last if $href->{'active_flag'} == 0;
	}

	$href->{'insurance_desc2'} =~ s/ PART / /g;
	$href->{'insurance_desc3'} =~ s/ PART / /g;
	if ($href->{'active_flag'} >= 0 ) {
		$href->{'patient_ssn'} = $flds->[79] if $flds->[78] =~ /^SSN/i ;
	}
}



#----------------------------------------------------
# Handler for 'WASHINGTON DC MEDICAID'
#
#----------------------------------------------------
sub hdl_00009 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i &&
			$href->{'active_flag'} < 0      &&
			$flds->[5+$i] =~ /Mcaid/i )
		{
			$href->{'active_flag'} = 1;
			if( $flds->[20+$i] =~ /^Elig/i ) {
				my($s, $e) = split(/-/, $flds->[21+$i]);
				$href->{'effective_date'} = $s;
				$href->{'effective_date_end'} = $e;
			}
		}
		elsif ( $flds->[$i] =~ /Svces\s+Restricted\s+to\s+Following\s+Prov/i && 
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i && 
				$flds->[5+$i] =~ /Mcaid/i &&
				$flds->[38+$i] =~ /(Contracted|Prov)/i ) 
		{ 
			next if $flds->[39+$i] =~ /(PHARM|RX|CVS|Walgreens)/i;
			if ( ! exists $href->{'c_insurance_desc1'} ) {
				$href->{'c_insurance_desc1'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'c_effective_date1'} = $s;
					$href->{'c_effective_date_end1'} = $e;
				}
				if( $flds->[57+$i] =~ /Phone/i ) {
					$href->{'c_contact_phone1'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'c_insurance_desc2'} ) {
				$href->{'c_insurance_desc2'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'c_effective_date2'} = $s;
					$href->{'c_effective_date_end2'} = $e;
				}
				if( $flds->[57+$i] =~ /Phone/i ) {
					$href->{'c_contact_phone2'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ($flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}

		if ($flds->[86] =~ /^Elig/i  and 
			$href->{'effective_date'} == undef ) 
		{
			my($s, $e) = split(/-/, $flds->[87]);
			$href->{'effective_date'}     = $s;
			$href->{'effective_date_end'} = $e;
		}
	}
}

#-------------------------------------------------
# handler for 'KENTUCKY MEDICAID' data.
#
#   Chunk index abnormal
#-------------------------------------------------
sub hdl_00014{
	my $href = shift;
	my $flds = shift;

	#foreach my $i (86,147,206,265,324,383,442,501,560, @chunks) { 
        #foreach my $i (93,154,213,272,331,390,449,508,567, @chunks) {
         foreach my $i (108,169,228,287,346,405,464,523,582, @chunks) {
		if( $flds->[$i] =~ /Inactv/i &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[5+$i] =~ /Mcaid/i       && 
			$href->{'active_flag'} < 0      &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[25+$i] . " " . $flds->[26+$i];
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[5+$i] =~ /Mcare|Medicare/i   && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			next if $flds->[5+$i] =~ /care\s+D/i;          # ignore Mcare D
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[5+$i];
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		$href->{'patient_ssn'} = $flds->[79] if $flds->[78] =~ /SSN/i;
	}
}


#----------------------------------------------------
# Handler for 'ARKANSAS MEDICAID'
#
#----------------------------------------------------
sub hdl_00015 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( 108 ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i ){
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[8+$i];
			if ($flds->[20+$i] =~ /^Elig/i ) {
				my($s, $e) = split(/-/, $flds->[21+$i]);
				$href->{'effective_date'}     = $s;
				$href->{'effective_date_end'} = $e;
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}




#----------------------------------------------------
# Handler for 'CONNECTICUT MEDICAID'
#
#----------------------------------------------------

sub hdl_00156 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) {
		if ( $flds->[$i] =~ /Inactv/i ) {
			$href->{'active_flag'} = 0;
			last;
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i && $flds->[3+$i] =~ /Health Bene Plan Cvg/i ){
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[8+$i];
		}
                elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && ( $flds->[5+$i] =~ /Mcare A/i || $flds->[5+$i] =~ /Mcare B/i) )
                {
                        if ( ! exists $href->{'insurance_desc2'} ) {
                                $href->{'subscriber_id2'} = $flds->[16+$i];
                                if ($flds->[5+$i] =~ /Mcare A/i) {$href->{'insurance_desc2'} = "MEDICARE A";}
                                else                             {$href->{'insurance_desc2'} = "MEDICARE B";}
                        }
                        elsif ( ! exists $href->{'insurance_desc3'} ) {
                                $href->{'subscriber_id3'} = $flds->[16+$i];
                                if ($flds->[5+$i] =~ /Mcare A/i) {$href->{'insurance_desc3'} = "MEDICARE A";}
                                else                             {$href->{'insurance_desc3'} = "MEDICARE B";}
                        }
                }
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}




#----------------------------------------------------
# Handler for 'CALIFORNIA ST MEDICAID' 
#
#----------------------------------------------------


sub hdl_00016 {
	my $href = shift;
	my $flds = shift;

         foreach my $i ( @chunks) {
                if( $flds->[$i] =~ /Inactv/i &&
                        $flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i )
                {
                        $href->{'active_flag'} = 0;
                        last;
                }
                elsif( $flds->[$i] =~ /Actv\s+Cvg/i &&
                        ( $flds->[5+$i] =~ /Mcaid/i || ( $flds->[5+$i] =~ /Other/i && $flds->[8+$i] =~ /CMSP/i ) ) &&
                        $href->{'active_flag'} < 0      &&
                        $flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i )
                {
                        $href->{'active_flag'} = 1;
                        $href->{'plan_type'} = $flds->[8+$i];
                        if ( $href->{'plan_type'} =~ /CMSP/i ) { $href->{'plan_type'} = "CMSP - COUNTY MEDICAL SERVICES PROGRAM"; }
                }

                elsif( $flds->[$i] =~ m#Other/Addl\s+Payor#i &&
                       $href->{'active_flag'} == 1             &&
                       $flds->[37+$i] =~ /Benefit\s+Related\s+Entity/i &&
                       $flds->[38+$i] =~ /Plan\s+Sponsor/i &&
                       $flds->[3+$i] =~ /Med\s+Care/i )
                {
                        if ( ! exists $href->{'insurance_desc2'} ) {
                                $href->{'insurance_desc2'} = $flds->[39+$i];
                                if ( $flds->[57+$i] =~ /Phone/i ) {
                                    $href->{'insurance_contact_phone2'} =  $flds->[58+$i];
                                }
                        }
                        elsif ( ! exists $href->{'insurance_desc3'} ) {
                                next if $href->{'insurance_desc2'} eq $flds->[39+$i];
                                $href->{'insurance_desc3'} = $flds->[39+$i];
                                if ( $flds->[57+$i] =~ /Phone/i ) {
                                    $href->{'insurance_contact_phone3'} =  $flds->[58+$i];
                                }
                        }
                }
                elsif(  $flds->[$i] =~ /MC\s+Coord/  &&
                        $href->{'active_flag'} == 1             &&
                        $flds->[37+$i] =~ /Benefit\s+Related\s+Entity/i && 
                        $flds->[38+$i] =~ /Plan\s+Sponsor/i &&
                        $flds->[3+$i] =~ /Med\s+Care/i )
                {
                        if ( ! exists $href->{'c_insurance_desc1'} ) {
                                $href->{'c_insurance_desc1'} =  $flds->[39+$i];
                                if ( $flds->[57+$i] =~ /Phone/i ) {
                                    $href->{'c_contact_phone1'} =  $flds->[58+$i]; 
                                }
                        }
                }
        

        }

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
                elsif (  ( $flds->[3] eq $flds->[61] ) || ( $flds->[3] eq $flds->[79] ) ) {
                        $href->{'patient_ssn'} = $flds->[3];
                }

                if (  (substr($flds->[4],0,6)) eq (substr($flds->[47],0,6)) ) {

                       #correct the fact that last names are truncated at 6 bytes
                       $href->{'patient_lname'} = $flds->[4];
                       $href->{'subscriber_lname'} = $flds->[4];
                }
	}
}

#--------------------------------------------------------------------------
# handler for TENNESSEE MEDICAID / TENNCARE data.
#
#  Must reset 'subscriber_id' .
#--------------------------------------------------------------------------
sub hdl_00017{
	my $href = shift;
	my $flds = shift;

	foreach my $i (@chunks) { 
		next if $flds->[$i] =~ /^(Co-Pay)/i;
		if ( $flds->[$i] =~ /Inactv/i &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[5+$i] =~ /HMO/i &&
			$href->{'active_flag'} < 0	&&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			$href->{'active_flag'} = 1;
			$href->{'plan_type'}   = "$flds->[5+$i] - $flds->[8+$i]";
		} 
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[5+$i] =~ /(Mcare|Medicare)/i &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			next if $flds->[5+$i] =~ /care\s+D/i;          # ignore Mcare D
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'insurance_desc2'} = $flds->[8+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[8+$i];
				$href->{'insurance_desc3'} = $flds->[8+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
			}
		}
		elsif( $flds->[$i] =~ m#Other/Addl\s+Payor#i && 
			$flds->[38+$i] =~ /Contracted\s+Svc\s+Prov/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'insurance_desc2'} = $flds->[39+$i];
				$href->{'subscriber_id2'}  = $flds->[16+$i];
				if( $flds->[20+$i] =~ /^Elig|COB/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[39+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				$href->{'subscriber_id3'}  = $flds->[16+$i];
				if( $flds->[20+$i] =~ /^Elig|COB/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
			}
		}
		elsif( $flds->[$i] =~ /MC\s+Coord/  && 
			$href->{'active_flag'} == 1		&&
			$flds->[5+$i] =~ /HMO/i 		&& 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			if ( ! exists $href->{'c_insurance_desc1'} ) {
				$href->{'c_insurance_desc1'} =  $flds->[8+$i];
			}
			elsif ( ! exists $href->{'c_insurance_desc2'} ) {
				next if $href->{'c_insurance_desc1'} eq  $flds->[8+$i];
				$href->{'c_insurance_desc2'} =  $flds->[8+$i];
			}
		}
	}

	if ($href->{'active_flag'} < 0 && 
		$href->{'insurance_desc2'} =~ /\w/ ) 
	{
		# keep the record to show useful Addl Payor info 
		$href->{'active_flag'} = 0;  
	}

		# reset 'subscriber_id' .
	if ($href->{'active_flag'} >= 0 ) {
		if( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		} elsif( $flds->[78] =~ /^Mcaid\s+Recip\s+ID/i ) {
			$href->{'subscriber_id'}  = $flds->[79];
		}

		if( $flds->[80] =~ /^Mcaid\s+Recip\s+ID/i ) {
			$href->{'subscriber_id'}  = $flds->[81];
		}
		elsif( $flds->[80] =~ /^Health.*HIC/ and ! $href->{'patient_ssn'} ) {
			$href->{'patient_ssn'} = $flds->[81];
			$href->{'patient_ssn'} =~ s/^(\d{9}).*$/$1/;
		}
	}
}



#----------------------------------------------------
# Handler for 'NORTH CAROLINA MEDICAID'
#
#----------------------------------------------------
sub hdl_00019 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		if( $flds->[$i] =~ /Inactv/i &&
			$flds->[3+$i] =~ /Health\sBene\sPlan\sCvg/i  ) 
		{ 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i &&
			$flds->[5+$i] =~ /(Mcaid|Medicaid)/i &&
			$href->{'active_flag'} < 0	&&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i  ) 
		{
			$href->{'active_flag'} = 1;
			if( $flds->[5+$i] =~ /^Mcaid/i ) {
				$href->{'plan_type'} = "MEDICAID - $flds->[8+$i]";
			} else {
				$href->{'plan_type'} = $flds->[8+$i];
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[38+$i] =~ /Pay.r/i &&
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i  ) 
		{
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^(Bene|Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[39+$i];
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^(Bene|Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[5+$i] =~ /(Mcare|Medicare)/i &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			next if $flds->[5+$i] =~ /care\s+D/i;          # ignore Mcare D
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'insurance_desc2'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[5+$i];
				$href->{'insurance_desc3'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
		elsif ( $flds->[80] =~ /^Health.*HIC/ ) {
			$href->{'patient_ssn'} = $flds->[81];
			$href->{'patient_ssn'} =~ s/^(\d{9}).*$/$1/;
		}
	}
}



#---------------------------------------------------------------------
# Handler for New York State Medicaid.
#
#   NY State using "RECERT MONTH=dd" for renewing. It means the person
#   should recertificate the MEDICAID the month next year. Here this
#   information is used to estimate the expiration date for active acct.
#---------------------------------------------------------------------
sub hdl_00023 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i || ($flds->[$i] =~ /Actv\s+Cvg/i && $flds->[61] eq "") ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			$href->{'active_flag'} = 1;
			if($flds->[5+$i] =~ /(Mcaid|Medicaid)/i && $flds->[8+$i] ne "") {
				$href->{'plan_type'} = "MEDICAID - " . $flds->[8+$i];
			}
			if ($flds->[20+$i] =~ /^Bene\s*Begin/i ) {
				$href->{'effective_date'}  = $flds->[21+$i];
				my $b_mon;
				my( $a_mon, $a_day, $a_yr) = split(/\//, $href->{'effective_date'});
				for my $n (25 .. 30) {   # search multi fields
					if ($flds->[$n+$i] =~ /^RECERT/i ) { 
						$b_mon = $flds->[$n+$i] ;
						last;
					}
				}
				if( $b_mon =~ /\d/ ) {
					$b_mon =~ s/^.*=//;
					$a_yr ++;
					if( $b_mon eq "02" && ($a_day eq "29" || $a_day eq "30" || $a_day eq "31") ) {
						$a_day = "28";
					}
					elsif( ($b_mon eq "04" || $b_mon eq "06" || $b_mon eq "09" || $b_mon eq "11") 
							&& $a_day eq "31") 
					{
						$a_day = "30";
					}
					$href->{'effective_date_end'} = "$b_mon/$a_day/$a_yr";
				}
			}
		}
		elsif($flds->[$i] =~ /MC\s+Coord/i  && 
			  $flds->[5+$i] =~ /(Mcaid|Medicaid)/i ) 
		{
			if($flds->[38+$i] =~ /Admin/i ) {
				$href->{'c_insurance_desc1'} = $flds->[39+$i];
			}
			if($flds->[57+$i] =~ /Phone/i) {
				$href->{'c_contact_phone1'} = $flds->[58+$i];
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[38+$i] =~ /Pay.r/i &&
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^Elig|Bene/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^Elig|Bene/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
	}

	$href->{'patient_ssn'} = $flds->[79] if $flds->[78] =~ /^SSN/i;

        if ( $flds->[700] =~ /NAME/i ) {
        
           $href->{'patient_lname'} = $href->{'input_lname'};
           $href->{'patient_fname'} = $href->{'input_fname'};
           $href->{'subscriber_lname'} = $href->{'input_lname'};
           $href->{'subscriber_mname'} = $href->{'input_mname'};
           $href->{'subscriber_fname'} = $href->{'input_fname'};

        }
}



#----------------------------------------------------
# Handler for 'WASHINGTON STATE MEDICAID'
#
#  Some record use [109] instead of [108]
#----------------------------------------------------
sub hdl_00025 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( 109, @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i &&
			$flds->[5+$i] =~ /Mcaid/i   && 
			$href->{'active_flag'} < 0	&&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i  ) 
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[8+$i];
			
			if( $flds->[20+$i] =~ /^Elig/i ) {
				my($s, $e) = split(/-/, $flds->[21+$i]);
				$href->{'effective_date'} = $s;
				$href->{'effective_date_end'} = $e;
			}
			if( $flds->[15+$i] =~ /^Grp\s+(\#|Num)/i ) {
				$href->{'group_number'}  = $flds->[16+$i];
			}
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[5+$i] =~ /(Mcare|Medicare)/i &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			next if $flds->[5+$i] =~ /care\s+D/i;          # ignore Mcare D
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'insurance_desc2'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[5+$i];
				$href->{'insurance_desc3'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[38+$i] =~ /Pay.r/i &&
				$flds->[5+$i] =~ /Comm/i &&
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			next if $flds->[16+$i] =~ /MED(ICARE)?\s+PART\s+D/i;
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^(Bene|Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[39+$i];
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^(Bene|Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
		elsif ( $flds->[$i] =~ /Svces\s+Restricted\s+to\s/i && 
				$flds->[38+$i] =~ /(Contracted|Prov)/i ) 
		{ 
			next if $flds->[39+$i] =~ /(PHARM|RX|CVS|Walgreens)/i;
			if ( ! exists $href->{'c_insurance_desc1'} ) {
				$href->{'c_insurance_desc1'} =  $flds->[39+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'c_effective_date1'} = $s;
					$href->{'c_effective_date_end1'} = $e;
				}
				if( $flds->[57+$i] =~ /Phone/i ) {
					$href->{'c_contact_phone1'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'c_insurance_desc2'} ) {
				$href->{'c_insurance_desc2'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'c_effective_date2'} = $s;
					$href->{'c_effective_date_end2'} = $e;
				}
				if( $flds->[57+$i] =~ /Phone/i ) {
					$href->{'c_contact_phone2'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
		elsif ( $flds->[78] =~ /^Health.*HIC/ ) {
			$href->{'patient_ssn'} = $flds->[79];
			$href->{'patient_ssn'} =~ s/^(\d{9}).*$/$1/ ;
		}
	}
}


#----------------------------------------------------
# Handler for 'WEST VIRGINIA MEDICAID'
#
#----------------------------------------------------
sub hdl_00027{
	my $href = shift;
	my $flds = shift;

	foreach my $i (@chunks) { 
		if ( $flds->[$i] =~ /Inactv/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i )  
		{
			$href->{'active_flag'} = 0;
			;#last;		# comment out for QMB searching
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[5+$i] =~ /Mcaid|QMB/i && 
			$href->{'active_flag'} <= 0   &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i )  
		{
			$href->{'active_flag'} = 1 if $flds->[5+$i] =~ /Mcaid/i;
			$href->{'plan_type'}   = $flds->[5+$i] . " - " . $flds->[8+$i];
		} 
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[5+$i] =~ /HMO|COMM|Mcare/i &&
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			if ( ! exists $href->{'insurance_desc2'} ) {
				if( $flds->[5+$i] =~ /Mcare/i ) {
					$href->{'insurance_desc2'} = $flds->[5+$i];
				}
				elsif( $flds->[38+$i] =~ /sponsor/i ) {
					if( $flds->[39+$i] =~ /^(HMO|TPL)/i ) {
						$href->{'insurance_desc2'} = $flds->[56+$i]
					} else {
						$href->{'insurance_desc2'} = $flds->[39+$i]
					}
				}
				if( $flds->[20+$i] =~ /^(Bene|Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				if( $flds->[5+$i] =~ /Mcare/i ) {
					$href->{'insurance_desc3'} = $flds->[5+$i];
				}
				elsif( $flds->[38+$i] =~ /sponsor/i ) {
					if( $flds->[39+$i] =~ /^(HMO|TPL)/i ) {
						$href->{'insurance_desc3'} = $flds->[56+$i]
					} else {
						$href->{'insurance_desc3'} = $flds->[39+$i]
					}
				}
				if( $flds->[20+$i] =~ /^(Bene|Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		$href->{'patient_ssn'}  = $flds->[79] if $flds->[78] =~ /SSN/i;
	}
}


#---------------------------------------------------------------------
# Handler for Unitedcare data.
#
# The status of a record is decided by the first chunk of data.
# field[89] always has a value of 'Health Bene Plan Cvg'. If field[86]
# is 'Inactv', the record is inactive; if field[86] is 'Actv Cvg',
# and field[91] is ether 'Comm' or 'Mcaid', the record is active. 
#
# For few times, field[91] is empty. If field[94] has a value of 'HMO',
# field[94] is used to decide if record is active.
#
#---------------------------------------------------------------------
sub hdl_00029 {
	my $href = shift;
	my $flds = shift;

	foreach my $i (108) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			$href->{'active_flag'} = 1;
           
                        if ($flds->[5+$i] =~ /Mcare/i || $flds->[8+$i] =~ /MEDICARE/i) 
                           {
			     $href->{'plan_type'}  = "MCARE HMO - " . $flds->[8+$i];
                             $href->{'medlytix_ins_type'}  = "M";
                           }
			elsif ($flds->[5+$i] =~ /Mcaid/i) 
                           {
				$href->{'plan_type'} = "MCAID HMO - " . $flds->[8+$i] . " - " . $flds->[9+$i];
                                $href->{'medlytix_ins_type'}  = "D";
			   }
                        else
                           {
                                $href->{'plan_type'} = $flds->[8+$i] . " - " . $flds->[9+$i];
                                $href->{'medlytix_ins_type'}  = "C";
                           }
		}
	}

        if ($href->{'active_flag'} >= 0 ) {

                if ($flds->[78] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Prior\s+ID/i && $flds->[79] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[79];
                }


                if ($flds->[80] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Prior\s+ID/i && $flds->[81] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[81];
                }


                if ($flds->[82] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Prior\s+ID/i && $flds->[83] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[83];
                }


                if ($flds->[84] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Prior\s+ID/i && $flds->[85] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[85];
                }


                if ($flds->[86] =~ /^Elig/i ) {
                        my($s, $e) = split(/-/, $flds->[87]);
                        $href->{'effective_date'}     = $s;
                        $href->{'effective_date_end'} = $e;
                }
        }

}


#--------------------------------------------------------------------
# Handler for 'ILLINOIS MEDICAID'
#
#--------------------------------------------------------------------
sub hdl_00032 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/ and
				$flds->[25+$i] =~ /^\s*\d+\s/ )
		{ 
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[25+$i] if $flds->[25+$i] =~ /\w/;
			if( $flds->[20+$i] =~ /Elig/i ) {
				my($s, $e) = split(/-/, $flds->[21+$i]);
				$href->{'effective_date'}     = $s if $s =~ /\d/;
				$href->{'effective_date_end'} = $e if $e =~ /\d/;
			}
		}
		elsif($flds->[$i] =~ /Limitations/i && 
			$flds->[2+$i] =~ /Individual/i  && 
			$flds->[3+$i] =~ /Emergency\s+S/i )
		{ 
			$href->{'active_flag'} = 1;
			if( $flds->[25+$i] =~ /\w/) {
				$href->{'plan_type'} = $flds->[25+$i];
			} else {
				$href->{'plan_type'} = "LIMITATIONS: EMERGENCY SVCS ONLY";

			}
			if( $flds->[20+$i] =~ /Elig/i ) {
				my($s, $e) = split(/-/, $flds->[21+$i]);
				$href->{'effective_date'}     = $s if $s =~ /\d/;
				$href->{'effective_date_end'} = $e if $e =~ /\d/;
			}
		}
		elsif($flds->[$i] =~ /Limitations/i &&
			$flds->[2+$i] =~ /Individual/i  &&
			$flds->[3+$i] =~ /General Benefits/i )
		{
			$href->{'active_flag'} = 1;
			if( $flds->[25+$i] =~ /\w/) {
				$href->{'plan_type'} = $flds->[25+$i];
			} else {
				$href->{'plan_type'} = "LIMITATIONS : GENERAL BENEFITS";
			}
			if( $flds->[20+$i] =~ /Elig/i ) {
				my($s, $e) = split(/-/, $flds->[21+$i]);
				$href->{'effective_date'}     = $s if $s =~ /\d/;
				$href->{'effective_date_end'} = $e if $e =~ /\d/;
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i && 
				$flds->[5+$i] =~ /HMO/i &&
				$flds->[38+$i] =~ /Contracted\s+Svc\s+Prov/i ) 
		{ 
			if ( ! exists $href->{'c_insurance_desc1'} ) {
				$href->{'c_insurance_desc1'} =  $flds->[39+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'c_effective_date1'} = $s;
					$href->{'c_effective_date_end1'} = $e;
				}
				if( $flds->[57+$i] =~ /Phone/i ) {
					$href->{'c_contact_phone1'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'c_insurance_desc2'} ) {
				$href->{'c_insurance_desc2'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'c_effective_date2'} = $s;
					$href->{'c_effective_date_end2'} = $e;
				}
				if( $flds->[57+$i] =~ /Phone/i ) {
					$href->{'c_contact_phone2'} = $flds->[58+$i];
				}
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[38+$i] =~ /Pay.r/i &&
				$flds->[3+$i] !~ /(VISION|VIEW|DENTAL|DNTL|PHARM|RX|Chiropractic)/i )
		{
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i && 
				$flds->[5+$i] =~ /Mcare|Medicare/i )
		{
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		$href->{'patient_ssn'}  = $flds->[79] if $flds->[78] =~ /^SSN/i;
	}
}


#--------------------------------------------------------------------
# Handler for Tricare data
#
# The status of a record is decided by the first chunk of data.
# field[89] always has a value of 'Health Bene Plan Cvg'. If field[86]
# is 'Inactv', the record is inactive; if field[86] is 'Actv Cvg',
# and field[91] is ether 'HMO' or 'Indemnity', the record is active. 
#
#--------------------------------------------------------------------
sub hdl_00033 {
	my $href = shift;
	my $flds = shift;

	foreach my $i (108) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			$href->{'active_flag'} = 1;
			$href->{'plan_type'}   = "$flds->[5+$i] - $flds->[8+$i]";
                        $href->{'medlytix_ins_type'}   = "C";
		}
	}

	if ($href->{'active_flag'} >= 0 ) {

                if ($flds->[78] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Prior\s+ID/i && $flds->[79] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[79];
                }


                if ($flds->[80] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Prior\s+ID/i && $flds->[81] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[81];
                }


                if ($flds->[82] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Prior\s+ID/i && $flds->[83] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[83];
                }

                if ($flds->[84] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Prior\s+ID/i && $flds->[85] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[85];
                }

	}
}



#----------------------------------------------------
# Handler for 'FLORIDA MEDICAID'
#
#  QMB --  Qualified Medicare Beneficairy 
#  It is a kind of MCaid.
#
#  PCP --  Primary Care Provider 
#  		  | Primary Care Practitioner
#  		  | Primary Care Physician
#----------------------------------------------------
sub hdl_00036 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[5+$i] =~ /Mcaid|QMB/i && 
			$href->{'active_flag'} < 0   &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[8+$i] . " " . $flds->[9+$i];
			$href->{'plan_type'} =~ s/\|/-/g;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[5+$i] =~ /Mcare|Medicare/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			next if $flds->[5+$i] =~ /care\s+D/i ;
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'insurance_desc2'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^(Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[5+$i];
				$href->{'insurance_desc3'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^(Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
			}
		}
		elsif($flds->[$i] =~ /PCP/i && 
			($flds->[8+$i] =~ /MEDICAID\s+HMO/i || $flds->[8+$i] =~ /MEDICAID\s+REFORM\s+HMO/i) ) 
		{
			$href->{'c_insurance_desc1'} = $flds->[39+$i];
			if($flds->[57+$i] =~ /Phone/i) {
				$href->{'c_contact_phone1'} = $flds->[58+$i];
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}



#----------------------------------------------------
# Handler for 'OKLAHOMA MEDICAID'
#
#----------------------------------------------------
sub hdl_00037 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			($href->{'active_flag'} < 0 or $href->{'plan_type'} =~ /MENTAL HEALTH/i)   &&
				$flds->[8+$i] ne "" &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[8+$i];
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[38+$i] =~ /Pay.r/i &&
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			next if	$flds->[8+$i] =~ /Pharm|RX/i; 
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^(COB|Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[39+$i];
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^(COB|Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}


#----------------------------------------------------
# Handler for 'ALABAMA MEDICAID'
#
#	No valid data for test.
#----------------------------------------------------
sub hdl_00038 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$href->{'active_flag'} < 0 	   &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[8+$i];

			if ($flds->[20+$i] =~ /^Elig/i ) {
				my($s, $e) = split(/-/, $flds->[21+$i]);
				$href->{'effective_date'}     = $s;
				$href->{'effective_date_end'} = $e;
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[38+$i] =~ /Pay.r/i &&
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			next if	$flds->[8+$i] =~ /Pharm|RX|Drug|Dental|Vision|Optical/i; 
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[39+$i];
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
		elsif ( $flds->[78] =~ /^Health.*HIC/ ) {
			$href->{'patient_ssn'} = $flds->[79];
			$href->{'patient_ssn'} =~ s/^(\d{9}).*$/$1/;
		}
	}
}



#----------------------------------------------------
# Handler for 'PENNSYLVANIA MEDICAID'
#
#  Some chunk starts at [169], [228]
#
#  Remarks:  Srvcs Capitated HMO
#----------------------------------------------------
sub hdl_00039 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( 169, 228, @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif( $flds->[$i] =~ /Actv\s+(Cvg|-)/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			$href->{'active_flag'} = 1;
			if( $flds->[5+$i] =~ /Mcaid/i ) {
				$href->{'plan_type'} = "MEDICAID - " . $flds->[8+$i];
				$href->{'insurance_contact_phone'} = $flds->[58+$i] if $flds->[57+$i] =~ /Phone/i;
			} 
			elsif( $flds->[5+$i] =~ /HMO/i ) {
				if( $flds->[38+$i] =~ /Payer/i ) {
					$href->{'c_insurance_desc1'} = $flds->[39+$i];
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'c_contact_phone1'} = $flds->[58+$i];
				}
				foreach my $j (20, 22) {
					$href->{'c_effective_date1'} = $flds->[1+$i+$j] if $flds->[$i+$j] =~ /^Elig/i; 
				}
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[38+$i] =~ /Pay.r/i &&
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			next if $flds->[39+$i] =~ /Med(icare)?\s+Part\s+D/i;
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[39+$i];
				foreach my $j (20, 22) {
					$href->{'effective_date2'} = $flds->[1+$i+$j] if $flds->[$i+$j] =~ /^Elig/i; 
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[39+$i];
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				foreach my $j (20, 22) {
					$href->{'effective_date3'} = $flds->[1+$i+$j] if $flds->[$i+$j] =~ /^Elig/i; 
				}
				
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}




#----------------------------------------------------
# Handler for 'MISSISSIPPI MEDICAID'
#
#----------------------------------------------------
sub hdl_00040 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$href->{'active_flag'} < 0	  &&
			$flds->[5+$i] =~ /Mcaid|(Mcare\s+Primary)/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[8+$i];
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[5+$i] =~ /(Mcare|Medicare)/i ) 
		{
			next if	$flds->[5+$i] =~ /care\s+D/i ; 
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[8+$i];
				if ($flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'}     = $s;
					$href->{'effective_date_end2'} = $e;
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[8+$i];
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[8+$i];
				if ($flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'}     = $s;
					$href->{'effective_date_end3'} = $e;
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
		if ($flds->[86] =~ /^Elig/i ) {
			my($s, $e) = split(/-/, $flds->[87]);
			$href->{'effective_date'}     = $s;
			$href->{'effective_date_end'} = $e;
		}
	}
}



#----------------------------------------------------
# Handler for 'NEW MEXICO MEDICAID'
#
#----------------------------------------------------
sub hdl_00041 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( 108 ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[8+$i];
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}

#---------------------------------------------------------------------
# Handler for Cigna data.
#
# Some of the record inactive information are not in first chunk,
# so multiple chunks are searched.
#
# If the chunk's first field is 'Inactv', and the fourth field is
# 'Health Bene Plan Cvg', the record is inactive, and search stops.
#
# If the chunk's first field is 'Actv Cvg', and fourth field is
# 'Health Bene Plan Cvg', the record is active, and search stops.
#
#
#-----------------------------------------------------
sub hdl_00042{
	my $href = shift;
	my $flds = shift;

	foreach my $i (@chunks) { 
		last if length($flds->[$i]) == 0;

		if ( $flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) { 
			if ( $flds->[$i] =~ /Inactv|Non-Cvd/i ) { 		# 'Non-Cvd' required
				$href->{'active_flag'} = 0;
			}
			elsif($flds->[$i] =~ /Actv\s+Cvg/i ){
				$href->{'active_flag'} = 1;

                                if ( $flds->[$i+67] =~ /MEDICARE/i ) 
                                  {
                                   $href->{'plan_type'}  = "MCARE HMO - " .  $flds->[$i+67];
                                   $href->{'medlytix_ins_type'}  = "M";
                                  }
                                else
                                  {
                                   $href->{'plan_type'}  =  "$flds->[5+$i] - $flds->[8+$i]";
                                   $href->{'medlytix_ins_type'}  = "C";
                                  }
			}
		}
		last if $href->{'active_flag'} >= 0 ; 
	}

        if ($href->{'active_flag'} >= 0 ) {

                if ($flds->[78] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Prior\s+ID/i && $flds->[79] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[79];
                }


                if ($flds->[80] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Prior\s+ID/i && $flds->[81] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[81];
                }


                if ($flds->[82] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Prior\s+ID/i && $flds->[83] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[83];
                }


                if ($flds->[84] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Prior\s+ID/i && $flds->[85] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[85];
                }


                if( $flds->[86] =~ /^Plan\s+Begin/i ) {
                        $href->{'effective_date'}     = $flds->[87];
                }
                if( $flds->[88] =~ /^Plan\s+End/i ) {
                        $href->{'effective_date_end'} = $flds->[89];
                }


        }

}




#----------------------------------------------------
# Handler for 'INDIANA MEDICAID'
#
#----------------------------------------------------
sub hdl_00045 {
	my $href = shift;
	my $flds = shift;

	foreach my $i (286,345,404,463,522, @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && $href->{'active_flag'} <0 ) {
			$href->{'active_flag'} = 1;
		}
		elsif($flds->[$i] =~ /Bene\sDescrip/i && $href->{'active_flag'}>0){
			$href->{'plan_type'} = $flds->[8+$i];
		}
		elsif($flds->[$i] =~ /MC\sCoord/i ) {
			if($flds->[38+$i] =~ /Sponsor/i ) {
				$href->{'c_insurance_desc1'} = $flds->[39+$i];
			}
			if($flds->[57+$i] =~ /Phone/i) {
				$href->{'c_contact_phone1'} = $flds->[58+$i];
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[5+$i] =~ /(Mcare|Medicare|Comm)/i ) 
		{
			next if	$flds->[5+$i] =~ /care\s+D/i ; 
			next if	$flds->[8+$i] =~ /Med(icare)?\s+Part\s+D|Dental|Pharm|RX|Vision|OPTICAL/i ; 
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				if ( $flds->[38+$i] =~ /\w/ ) {
					$href->{'insurance_desc2'} = $flds->[38+$i];
				} else {
					$href->{'insurance_desc2'} = $flds->[5+$i];
				}

				if ($flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'}     = $s;
					$href->{'effective_date_end2'} = $e;
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				$href->{'subscriber_id3'} = $flds->[16+$i];
				if ( $flds->[38+$i] =~ /\w/ ) {
					$href->{'insurance_desc3'} = $flds->[38+$i];
				} else {
					$href->{'insurance_desc3'} = $flds->[5+$i];
				}

				if ($flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'}     = $s;
					$href->{'effective_date_end3'} = $e;
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
		elsif ( $flds->[76] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[77];
		}
	}
}




#----------------------------------------------------
# Handler for 'NEW JERSEY MEDICAID'
#
#----------------------------------------------------
sub hdl_00046 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i &&
			$href->{'active_flag'} < 0	  &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i)
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[25+$i];
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i )
		{
			next if	$flds->[5+$i] =~ /care\s+D/i;
			next if	$flds->[25+$i] =~ /Dental|Pharm|OPTICAL|Vision/i;

			if(	$flds->[5+$i] =~ /HMO/i ) {
				if ( $flds->[17+$i] =~ /^Member\s+ID/i ) {$href->{'c_subscriber_id1'} = $flds->[18+$i];}
				$href->{'c_insurance_desc1'} = $flds->[39+$i];
				if ($flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'c_effective_date1'}     = $s;
					$href->{'c_effective_date_end1'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'c_contact_phone1'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				if( $flds->[5+$i] =~ /Mcare|Medicare/i) {
					$href->{'insurance_desc2'} = $flds->[5+$i];
				} else {
					$href->{'insurance_desc2'} = $flds->[39+$i];
				}
				if ($flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'}     = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $flds->[39+$i] eq $href->{'insurance_desc2'};
				$href->{'subscriber_id3'} = $flds->[16+$i];
				if( $flds->[5+$i] =~ /Mcare|Medicare/i) {
					$href->{'insurance_desc3'} = $flds->[5+$i];
				} else {
					$href->{'insurance_desc3'} = $flds->[39+$i];
				}

				if ($flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'}     = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}




#----------------------------------------------------
# Handler for 'LOUISIANA MEDICAID'
#
#----------------------------------------------------
sub hdl_00048 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( 108 ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[8+$i];
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}


#----------------------------------------------
# Handler for 'VIRGINIA MEDICAID / VMAP FHSC VA'
#
#----------------------------------------------
sub hdl_00049{
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) { 
				$href->{'active_flag'} = 0;
				last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$href->{'active_flag'} <0  &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			next if $flds->[8+$i] =~ /PHARM|RX|OPTICAL|VISION/i ;
			$href->{'active_flag'} = 1;
			$href->{'plan_type'}  = $flds->[8+$i];
		} 
		elsif($flds->[$i] =~ /Actv\s+Cvg/i &&
				$href->{'active_flag'} >= 0  &&
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i &&
				(! exists $href->{'c_insurance_desc1'}) &&
				$flds->[38+$i] =~ /Prov/i)
		{
			$href->{'c_insurance_desc1'} =  $flds->[39+$i];
			if($flds->[57+$i] =~ /Phone/i) {
					$href->{'c_contact_phone1'} = $flds->[58+$i];
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[38+$i] =~ /Pay.r/i &&
				$flds->[3+$i] =~ /(Health\s+Bene\s+Plan\s+Cvg|Medical)/i )
		{
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^(Bene|Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'}     = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $flds->[39+$i] eq $href->{'insurance_desc2'};
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^(Bene|Elig)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'}     = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}



#----------------------------------------------------
# Handler for 'OREGON MEDICAID'
#
#----------------------------------------------------
sub hdl_00054 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$href->{'active_flag'} <0  &&
			$flds->[5+$i] =~ /Mcaid/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[5+$i];
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[5+$i] =~ /Mcare|Medicare/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			next if $flds->[5+$i] =~ /care\s+D/i;          # ignore Mcare D
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $flds->[5+$i] eq $href->{'insurance_desc2'};
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^Elig/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}




#----------------------------------------------------
# Handler for 'ARIZONA MEDICAID'
#
#----------------------------------------------------
sub hdl_00056 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		next if $flds->[$i] =~ /^Bene\sDisclmr/;

		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$href->{'active_flag'} <0  &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i &&
                        $flds->[5+$i] =~ /Mcaid/i) 
		{ 
			$href->{'active_flag'} = 1;
			#$href->{'plan_type'} = $flds->[5+$i];
		}

                elsif( $flds->[$i] =~ m#Other/Addl\s+Payor#i &&
                        $flds->[5+$i] =~ /(Mcare A|Mcare B)/i ) 
                {
                        if ( ! exists $href->{'insurance_desc2'} ) {
                                $href->{'insurance_desc2'} = $flds->[39+$i];
                                $href->{'subscriber_id2'}  = $flds->[16+$i];
                                if( $flds->[20+$i] =~ /^Elig|COB/i ) {
                                        my($s, $e) = split(/-/, $flds->[21+$i]);
                                        $href->{'effective_date2'} = $s;
                                        $href->{'effective_date_end2'} = $e;
                                }
                        }
                        elsif ( ! exists $href->{'insurance_desc3'} ) {
                                next if $href->{'insurance_desc2'} eq $flds->[39+$i];
                                $href->{'insurance_desc3'} = $flds->[39+$i];
                                $href->{'subscriber_id3'}  = $flds->[16+$i];
                                if( $flds->[20+$i] =~ /^Elig|COB/i ) {
                                        my($s, $e) = split(/-/, $flds->[21+$i]);
                                        $href->{'effective_date3'} = $s;
                                        $href->{'effective_date_end3'} = $e;
                                }
                        }
                }

                elsif( $flds->[$i] =~ m#Other/Addl\s+Payor#i &&
                        $flds->[37+$i] =~ /Benefit\s+Related\s+Entity/i &&
                        $flds->[5+$i] =~ /Other/i )
                {
                        if ( ! exists $href->{'c_insurance_desc1'} ) {
                                $href->{'c_insurance_desc1'} = $flds->[39+$i];
                                $href->{'c_subscriber_id1'}  = $flds->[16+$i];
                                if( $flds->[20+$i] =~ /^Elig|COB/i ) {
                                        my($s, $e) = split(/-/, $flds->[21+$i]);
                                        $href->{'c_effective_date1'} = $s;
                                        $href->{'c_effective_date_end1'} = $e;
                                }
                        }
                }

	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}


#----------------------------------------------------
# Handler for 'TEXAS MEDICAID / HEALTHCARE'
#
#----------------------------------------------------
sub hdl_00066{
	my $href = shift;
	my $flds = shift;

	foreach my $i (108) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$href->{'active_flag'} <0  &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			$href->{'active_flag'} = 1;
			if ( $flds->[5+$i] =~ /Mcaid/i ) {
				$href->{'plan_type'}  = "MEDICAID - " . $flds->[8+$i];
			} else {
				$href->{'plan_type'}  =  $flds->[8+$i];
			}

			foreach my $j ( 20, 22) {
				if ($flds->[$i+$j] =~ /^Elig\s+Begin/i ) {
					$href->{'effective_date'}      = $flds->[1+$i+$j];
				}
				if ($flds->[$i+$j] =~ /^Elig\s+End/i ) {
					$href->{'effective_date_end'}  = $flds->[1+$i+$j];
				}
			}
		} 
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
		elsif ( $flds->[80] =~ /^Health.*HIC/ ) {
			$href->{'patient_ssn'} = $flds->[81];
			$href->{'patient_ssn'} =~ s/^(\d{9}).*$/$1/;
		}
	}
}




#----------------------------------------------------
# Handler for 'MISSOURI MEDICAID'
#
#----------------------------------------------------
sub hdl_00076{
	my $href = shift;
	my $flds = shift;

	foreach my $i (@chunks) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
		 	$flds->[3+$i] =~ /General\s+Benefit/i &&
			$href->{'active_flag'} <0 	&&
			$flds->[5+$i] =~ /MCaid/i )
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'}  = $flds->[8+$i] . " " . $flds->[10+$i];
		} 
		elsif($flds->[$i] =~ /Contact\s+Following\s+Entity\s+for\s+Elig/i &&
			$flds->[5+$i] =~ /HMO/i )  
		{
			 $href->{'c_insurance_desc1'} = $flds->[39+$i];
			
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[38+$i] =~ /Pay.r/i &&
				$flds->[8+$i] !~ /(VISION|VI|DENTAL|DNTL|PHARM|RX|OPT)/i )
		{

			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^(Elig|Bene)/i ) {
					$href->{'effective_date2'} = $flds->[21+$i];
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $flds->[39+$i] eq $href->{'insurance_desc2'};
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				if( $flds->[20+$i] =~ /^(Elig|Bene)/i ) {
					$href->{'effective_date3'} = $flds->[21+$i];
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[5+$i] =~ /Mcare|Medicare/i ) 
		{
			next if $flds->[5+$i] =~ /care\s+D/i;
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^(Elig|Bene)/i ) {
					$href->{'effective_date2'} = $flds->[21+$i];
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $flds->[5+$i] eq $href->{'insurance_desc2'};
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[5+$i];
				if( $flds->[20+$i] =~ /^(Elig|Bene)/i ) {
					$href->{'effective_date3'} = $flds->[21+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}


#----------------------------------------------------
# Handler for 'SOUTH CAROLINA MEDICAID'
#
#----------------------------------------------------
sub hdl_00081 {
	my $href = shift;
	my $flds = shift;

	foreach my $i ( @chunks ) { 
		next if $flds->[$i] =~ /^Bene\s+Descrip/i;
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i &&
			$href->{'active_flag'} <0 	)
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} .= " " . $flds->[8+$i];	# concat
		}
		elsif ( $flds->[25+$i] =~ /WITH\s+A?\s+MGD\s+CARE/i )  {
			if ( $flds->[38+$i] =~ /Prov/i )  {
				$href->{'c_insurance_desc1'} = $flds->[39+$i];
			}
			if ( $flds->[57+$i] =~ /Phone/i )  {
				$href->{'c_contact_phone1'} = $flds->[58+$i];
			}
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[5+$i] =~ /Mcare|Medicare/i ) 
		{
			next if $flds->[5+$i] =~ /care\s+D/i;
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[16+$i];
				$href->{'insurance_desc2'} = $flds->[8+$i];
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if $href->{'insurance_desc2'} eq $flds->[8+$i];
				$href->{'subscriber_id3'} = $flds->[16+$i];
				$href->{'insurance_desc3'} = $flds->[8+$i];
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
		$href->{'plan_type'} =~ s/^\s*//;
	}
}



#----------------------------------------------------
# Handler for 'COLORADO MEDICAID'
#
#----------------------------------------------------


sub hdl_00085 {
        my $href = shift;
        my $flds = shift;

        foreach my $i ( 108 ) {
                if ( $flds->[$i] =~ /Inactv/i ) {
                        $href->{'active_flag'} = 0;
                        last;
                }
                elsif( $flds->[$i] =~ /Actv\s+Cvg/i ){
                        $href->{'active_flag'} = 1;
                        $href->{'plan_type'} = $flds->[8+$i];
                }
        }

        if ($href->{'active_flag'} >= 0 ) {
                if ( $flds->[78] =~ /^SSN/i ) {
                        $href->{'patient_ssn'} = $flds->[79];
                }
        }
}


#----------------------------------------------------
# Handler for 'OHIO MEDICAID'
#
#----------------------------------------------------
sub hdl_00086{
	my $href = shift;
	my $flds = shift;

	foreach my $i (@chunks) { 
		if ( $flds->[$i] =~ /Inactv/i && 
			$flds->[5+$i] =~ /Mcaid/i ) 
		{ 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$href->{'active_flag'} <0  &&
			$flds->[5+$i] =~ /(MCaid)/i )
		{
			$href->{'active_flag'} = 1;
			$href->{'plan_type'}  = $flds->[8+$i];
		}
		elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
				$flds->[5+$i] =~ /Mcare|Medicare/i ) 
		{
			next if	$flds->[5+$i] =~ /care\s+D/i ; 
			next if	$flds->[8+$i] =~ /Flag:\s+N/i ; 
			if ( ! exists $href->{'insurance_desc2'} ) {
				$href->{'subscriber_id2'} = $flds->[18+$i];
				$href->{'insurance_desc2'} = $flds->[8+$i];
				if( $flds->[20+$i] =~ /^(Elig|Bene)/i ) {
					#$href->{'effective_date2'} = $flds->[21+$i];
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'}     = $s;
				}
			}
			elsif ( ! exists $href->{'insurance_desc3'} ) {
				next if	$flds->[8+$i] eq $href->{'insurance_desc2'} ; 
				$href->{'subscriber_id3'} = $flds->[18+$i];
				$href->{'insurance_desc3'} = $flds->[8+$i];
				if( $flds->[20+$i] =~ /^(Elig|Bene)/i ) {
					#$href->{'effective_date3'} = $flds->[21+$i];
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'}     = $s;
				}
			}
		}
		elsif ( $flds->[$i] =~ /MC\s+Coord/i  &&
				$flds->[5+$i] =~ /HMO/i	     &&  
				$flds->[38+$i] =~ /Prov/i )  
		{
				$href->{'c_insurance_desc1'} = $flds->[39+$i];
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
}




#----------------------------------------------------
# Handler for 'MICHIGAN MEDICAID'
#
#----------------------------------------------------
sub hdl_00090{
	my $href = shift;
	my $flds = shift;

	foreach my $i (@chunks) {
		if ( $flds->[108] =~ /Inactv/i &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{
			$href->{'active_flag'} = 0;
			last;
		}
		elsif( $flds->[$i] =~ /Actv\s+Cvg/i &&
                       ($href->{'active_flag'} <0 or ($flds->[3+$i] =~ /General Benefits/i && $flds->[5+$i] =~ /Mcaid/i)) && 
		       ($flds->[5+$i] =~ /Mcaid/i or $flds->[5+$i] =~ /Other/i )
		){
			$href->{'active_flag'} = 1;
			if ($flds->[5+$i] =~ /Mcaid/i) { 
				$href->{'plan_type'}  = "MEDICAID -" . $flds->[8+$i];
			} else { 
				$href->{'plan_type'}  = $flds->[8+$i];
			}
		}
                if ( $flds->[$i] =~ /Actv\s+Cvg/i && $flds->[3+$i] =~ /General Benefits/i && $flds->[5+$i] =~ /HMO/i )  {
                        if ( $flds->[38+$i] =~ /Payer/i )  {
                                $href->{'c_insurance_desc1'} = $flds->[39+$i];
                        }
                        if ( $flds->[57+$i] =~ /Phone/i )  {
                                $href->{'c_contact_phone1'} = $flds->[58+$i];
                        }
                }
                elsif ( $flds->[$i] =~ m#Other/Addl\s+Pay.r#i && ( $flds->[5+$i] =~ /Mcare A/i || $flds->[5+$i] =~ /Mcare B/i) )
                {
                        if ( ! exists $href->{'insurance_desc2'} ) {
                                $href->{'subscriber_id2'} = $flds->[16+$i];
                                if ($flds->[5+$i] =~ /Mcare A/i) {$href->{'insurance_desc2'} = "MEDICARE A";}
                                else                             {$href->{'insurance_desc2'} = "MEDICARE B";}
                        }
                        elsif ( ! exists $href->{'insurance_desc3'} ) {
                                $href->{'subscriber_id3'} = $flds->[16+$i];
                                if ($flds->[5+$i] =~ /Mcare A/i) {$href->{'insurance_desc3'} = "MEDICARE A";}
                                else                             {$href->{'insurance_desc3'} = "MEDICARE B";}
                        }
                }


	}

	if ($href->{'active_flag'} >= 0 ) {
		if ( $flds->[78] =~ /^SSN/i ) {
			$href->{'patient_ssn'} = $flds->[79];
		}
	}
        if ( $href->{'c_insurance_desc1'} ne "") { $href->{'plan_type'} = 'MEDICAID HMO';}
}



#----------------------------------------------------------------------
# Handler for Aetna data
#
# If a chunk starts with 'Other Sce of Data', process next chunk. 
#
# If a chunk starts with 'Inactv', and fields[3+$i] is
# 'Health Bene Plan Cvg', the record is inactive, and processing stops.
#
# If a chunk starts with 'Actv Cvg', and fields[3+$i] is
# 'Health Bene Plan Cvg', and field[5+$i] is one of
# 'HMO|PPO|POS|Indemnity|Exclusive Provider', the record is active. 
#----------------------------------------------------------------------
sub hdl_00092{
	my $href = shift;
	my $flds = shift;

	foreach my $i (@chunks) { 
		if ( $flds->[$i] =~ /Inactv/i && 
				$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i && 
				$href->{'active_flag'} < 0) 
		{
			$href->{'active_flag'} = 0;
			if ($flds->[20+$i] =~ /^Elig\s+End/i ) {
				$href->{'effective_date_end'}  = $flds->[21+$i];
			}
			last;
		}
		elsif (
			$flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i && 
			$flds->[5+$i] !~ /Other/i && 
			$flds->[5+$i] ne "" &&
			$href->{'active_flag'} < 0
		) 
		{
			$href->{'active_flag'} = 1;

			$href->{'plan_type'}  = $flds->[8+$i];
                        $href->{'medlytix_ins_type'}  = "C";
                        if ( $flds->[8+$i] =~ /MEDICARE/i ) 
                           {
                            $href->{'medlytix_ins_type'}  = "M";
                           }

			if ($flds->[20+$i] =~ /^Elig\s*$/i ) {
				$href->{'effective_date'}  = $flds->[21+$i];
			}
		}

		if($flds->[$i] =~ m#Other/Addl\s+Pay.r#i ) {
			if( $flds->[37+$i] =~ /Benefit\s+Related\s+Entity/i &&
				$flds->[38+$i] =~ /Pay.r/i  && 
				$flds->[3+$i] !~ /(VISION|VIEW|DENTAL|DNTL|PHARM|RX|Chiropractic)/i )
			{
				if( ! exists $href->{'subscriber_id2'} ) { 
					$href->{'insurance_desc2'} = "$flds->[39+$i] $flds->[40+$i]";
					$href->{'insurance_desc2'} =~ s/MDCR/MEDICARE/g ;
					$href->{'subscriber_id2'} = $flds->[43+$i] ;
				}
				elsif( ! exists $href->{'subscriber_id3'} ) { 
					$href->{'insurance_desc3'} = "$flds->[39+$i] $flds->[40+$i]";
					$href->{'insurance_desc3'} =~ s/MDCR/MEDICARE/g ;
					$href->{'subscriber_id3'} = $flds->[43+$i] ;
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {

                if ( $flds->[700] =~ /SSN\/DOB/i ) {
                        $href->{'patient_ssn'}   = $flds->[3]; 
                }

                if ($flds->[78] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Prior\s+ID/i && $flds->[79] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[79];
                }


                if ($flds->[80] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Prior\s+ID/i && $flds->[81] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[81];
                }


                if ($flds->[82] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Prior\s+ID/i && $flds->[83] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[83];
                }

               if ($flds->[84] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Prior\s+ID/i && $flds->[85] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[85];
                }

		if (! exists $href->{'effective_date'} ) {
			if( $flds->[86] =~ /^Elig/i ) {
				$href->{'effective_date'} = $flds->[87];
			}
		}
	}

}


#--------------------------------------------------------------------
# Handler for Hummana data.
#
# Only the first chunk of data is used to decide if the record is
# active.
#
# If field[86] is 'Inactv', the record is inactive; if field[86] is
# 'Actv Cvg', field[89] is 'Health Bene Plan Cvg',  the 
# record is active. 
#
# For TeamHealth, if field[91] is 'Mcaid' or field[111] contains
# 'MEDICARE', add 10,000 to the 'suppress_flag'.
#--------------------------------------------------------------------
sub hdl_00099 {
	my $href = shift;
	my $flds = shift;

	foreach my $i (108) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
				$href->{'active_flag'} = 0;
		}
                elsif ( $flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i &&
                                $flds->[$i] =~ /Actv\s+Cvg/i )
                {
                        $href->{'active_flag'} = 1;

                        if( $flds->[5+$i] =~/^Mcare/i || $flds->[25+$i] =~/^MEDICARE/i || $flds->[29+$i] =~/^MEDICARE/i) 
                           {
                             $href->{'medlytix_ins_type'} = "M";
                             $href->{'plan_type'} = "MCARE HMO - " . $flds->[25+$i] . " - " . $flds->[29+$i];
                           }
                        elsif ( $flds->[5+$i] =~/^Mcaid/i )

                           {
                             $href->{'medlytix_ins_type'} = "D";
                             $href->{'plan_type'} = "MCAID HMO - " . $flds->[8+$i] . " - " . $flds->[25+$i];
                           }
                        else
                           {
                             $href->{'medlytix_ins_type'} = "C";
                             $href->{'plan_type'} = $flds->[5+$i] . " - " . $flds->[25+$i] . " - " . $flds->[29+$i];
                           }

                }


	}

        if ($href->{'active_flag'} >= 0 ) {

                if ($flds->[78] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Prior\s+ID/i && $flds->[79] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[79];
                }


                if ($flds->[80] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Prior\s+ID/i && $flds->[81] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[81];
                }


                if ($flds->[82] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Prior\s+ID/i && $flds->[83] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[83];
                }


                if ($flds->[84] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Prior\s+ID/i && $flds->[85] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[85];
                }


                if ($flds->[86] =~ /^Elig/i ) {
                        $href->{'effective_date'}  = $flds->[87];
                }
        }

}


#---------------------------------------------------------------------
# Handler for  'NEVADA MEDICAID',
#---------------------------------------------------------------------
sub hdl_00116{
	my $href = shift;
	my $flds = shift;

	foreach my $i (108) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i &&
			$flds->[5+$i] =~ /Mcaid/i) 
		{ 
			$href->{'active_flag'} = 1;
			$href->{'plan_type'} = $flds->[8+$i];
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		$href->{'patient_ssn'}   = $flds->[79] if $flds->[78] =~ /^SSN/i;
	}
}


#-------------------------------------------------------------------
# Handler for BlueCrossBlueShield data.
#
# Most record status can be determined from first chunk data, but 
# fewer are in second or third chunk. So multiple chunk search is
# necessary.
#
#-------------------------------------------------------------------

sub hdl_00123{
	my $href = shift;
	my $flds = shift;

	foreach my $i (@chunks) { 
		last if length($flds->[$i]) == 0; 

		if ( $flds->[$i] =~ /(Inactv)/i ) { 
			$href->{'active_flag'} = 0;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$href->{'active_flag'} != 1    &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			$href->{'active_flag'} = 1;
			$href->{'plan_type'}  = "$flds->[5+$i] - $flds->[8+$i]";
                        $href->{'medlytix_ins_type'}  = "C";
                        if ( $flds->[8+$i] =~ /MEDICARE/i ) 
                           {
                            $href->{'medlytix_ins_type'}  = "M";
                           }
		}

		last if $href->{'active_flag'} >=0;
	}

	if ($href->{'active_flag'} >= 0 ) {

		if ($flds->[78] =~ /^Grp/i ) {
			$href->{'group_number'} = $flds->[79];
		}
                elsif ($flds->[78] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Prior\s+ID/i && $flds->[79] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[79];
                }


                if ($flds->[80] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Prior\s+ID/i && $flds->[81] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[81];
                }


                if ($flds->[82] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Prior\s+ID/i && $flds->[83] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[83];
                }


                if ($flds->[84] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Prior\s+ID/i && $flds->[85] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[85];
                }


		if ($flds->[86] =~ /^Elig/i ) {
			my($s, $e) = split(/-/, $flds->[87]);
			$href->{'effective_date'}     = $s;
			$href->{'effective_date_end'} = $e;
		}
	}
}



#----------------------------------------------------
# Handler for 'IDAHO MEDICAID'
#
#----------------------------------------------------

sub hdl_00175 {
        my $href = shift;
        my $flds = shift;

        foreach my $i ( @chunks ) {
                if ( $flds->[$i] =~ /Inactv/i &&
                     $flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i && 
                     $flds->[8+$i] !~ /DENTAL/i &&
                     $flds->[8+$i] !~ /VISION/i ) { 
                        $href->{'active_flag'} = 0;
                        last;
                }
                elsif($flds->[$i] =~ /Actv\s+Cvg/i &&
                        $flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i &&
                        $flds->[5+$i] =~ /Mcaid/i                        )
                {
                        $href->{'active_flag'} = 1;
                        $href->{'plan_type'} = $flds->[8+$i];
                }
        }

        if ($href->{'active_flag'} >= 0 ) {
                if ( $flds->[78] =~ /^SSN/i ) {
                        $href->{'patient_ssn'} = $flds->[79];
                }
        }
}




#---------------------------------------------------------------------
# Handler for  'Massachusetts MEDICAID'
#
#---------------------------------------------------------------------
sub hdl_00178{
	my $href = shift;
	my $flds = shift;

	foreach my $i (@chunks) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i && 
			$flds->[8+$i] !~ /^PREMIUM\s+ASSISTANCE/i ) 
		{ 
			$href->{'active_flag'} = 1;
			$href->{'plan_type'}   = $flds->[8+$i];
			if( $flds->[20+$i] =~ /^Elig/i ) {
				my($s, $e) = split(/-/, $flds->[21+$i]);
				$href->{'effective_date'} = $s;
				$href->{'effective_date_end'} = $e;
			}
		}
		elsif($flds->[$i] =~ m#Other/Addl\s+Pay.r#i && 
			$flds->[38+$i] =~ /Pay.r/i  && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i && 
			$flds->[8+$i] !~ /(VISION|VIEW|DENTAL|DNTL|PHARM|RX|Chiropractic)/i )
		{
			if( ! exists $href->{'subscriber_id2'} ) { 
				$href->{'insurance_desc2'} = $flds->[39+$i];
				$href->{'subscriber_id2'} = $flds->[16+$i] ;
				if( $flds->[20+$i] =~ /(Elig|COB)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date2'} = $s;
					$href->{'effective_date_end2'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone2'} = $flds->[58+$i];
				}
			}
			elsif( ! exists $href->{'subscriber_id3'} ) { 
				next if $href->{'insurance_desc2'} eq  $flds->[39+$i];
				$href->{'insurance_desc3'} = $flds->[39+$i];
				$href->{'subscriber_id3'} = $flds->[16+$i] ;
				if( $flds->[20+$i] =~ /(Elig|COB)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'effective_date3'} = $s;
					$href->{'effective_date_end3'} = $e;
				}
				if($flds->[57+$i] =~ /Phone/i) {
					$href->{'insurance_contact_phone3'} = $flds->[58+$i];
				}
			}
		}
		elsif( $flds->[$i] =~ /MC\s+Coord/  && 
			$href->{'active_flag'} == 1		&&
			$flds->[38+$i] =~ /Contracted\s+Svc\s+Prov/i  && 
			$flds->[8+$i] !~ /(VISION|VIEW|DENTAL|DNTL|PHARM|RX|Chiropractic)/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			if ( ! exists $href->{'c_insurance_desc1'} ) {
				$href->{'c_insurance_desc1'} =  $flds->[39+$i];
				if( $flds->[20+$i] =~ /(Elig|COB)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'c_effective_date1'} = $s;
					$href->{'c_effective_date_end1'} = $e;
				}
				if ( $flds->[57+$i] =~ /Phone/i )  {
					$href->{'c_contact_phone1'} = $flds->[58+$i];
				}
			}
			elsif ( ! exists $href->{'c_insurance_desc2'} ) {
				next if $href->{'c_insurance_desc1'} eq  $flds->[39+$i];
				$href->{'c_insurance_desc2'} =  $flds->[39+$i];
				if( $flds->[20+$i] =~ /(Elig|COB)/i ) {
					my($s, $e) = split(/-/, $flds->[21+$i]);
					$href->{'c_effective_date2'} = $s;
					$href->{'c_effective_date_end2'} = $e;
				}
				if ( $flds->[57+$i] =~ /Phone/i )  {
					$href->{'c_contact_phone2'} = $flds->[58+$i];
				}
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		$href->{'patient_ssn'}   = $flds->[77] if $flds->[76] =~ /^SSN/i;
		$href->{'patient_ssn'}   = $flds->[79] if $flds->[78] =~ /^SSN/i;
	}

}


#---------------------------------------------------------------------
# Handler for "Medica" data.
#
#---------------------------------------------------------------------

sub hdl_00196{
	my $href = shift;
	my $flds = shift;

	foreach my $i (108) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			$href->{'active_flag'} = 1;


                        if ($flds->[5+$i] =~ /Mcare/i || $flds->[8+$i] =~ /MEDICARE/i)
                           {
                             $href->{'plan_type'}  = "MCARE HMO - " . $flds->[8+$i];
                             $href->{'medlytix_ins_type'}  = "M";
                           }
                        elsif ($flds->[5+$i] =~ /Mcaid/i)
                           {
                                $href->{'plan_type'} = "MCAID HMO - " . $flds->[8+$i] . " - " . $flds->[9+$i];
                                $href->{'medlytix_ins_type'}  = "D";
                           }
                        else
                           {
                                $href->{'plan_type'} = $flds->[8+$i] . " - " . $flds->[9+$i];
                                $href->{'medlytix_ins_type'}  = "C";
                           }

		}
		 
	}

	if ($href->{'active_flag'} >= 0 ) {

                if ($flds->[78] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Prior\s+ID/i && $flds->[79] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[79];
                }
                elsif ($flds->[78] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[79];
                }


                if ($flds->[80] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Prior\s+ID/i && $flds->[81] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[81];
                }
                elsif ($flds->[80] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[81];
                }


                if ($flds->[82] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Prior\s+ID/i && $flds->[83] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[83];
                }
                elsif ($flds->[82] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[83];
                }


                if ($flds->[84] =~ /^Grp/i ) {
                        $href->{'group_number'} = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+(\#|Num)/i ) {
                        $href->{'plan_number'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^SSN/i ) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Prior\s+ID/i && $flds->[85] eq $flds->[3]) {
                        $href->{'patient_ssn'}  = $flds->[85];
                }
                elsif ($flds->[84] =~ /^Plan\s+Netw(or)?k/i ) {
                        $href->{'network_id'}    = $flds->[85];
                }


		if ($flds->[86] =~ /^Elig/i ) {
			my($s, $e) = split(/-/, $flds->[87]);
			$href->{'effective_date'}     = $s;
			$href->{'effective_date_end'} = $e;
		}
	}
}



#---------------------------------------------------------------------
# Handler for 'MARYLAND ST MEDICAID'
#
#---------------------------------------------------------------------

sub hdl_00240{
	my $href = shift;
	my $flds = shift;

	foreach my $i (@chunks) { 
		if ( $flds->[$i] =~ /Inactv/i ) { 
			$href->{'active_flag'} = 0;
			last;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i && 
			$href->{'active_flag'} < 0 && 
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i ) 
		{ 
			$href->{'active_flag'} = 1;
			if ($flds->[20+$i] =~ /^Elig\sBegin/i ) {
				$href->{'effective_date'}  = $flds->[21+$i];
			}
			if ($flds->[22+$i] =~ /^Elig\sEnd/i ) {
				$href->{'effective_date_end'} = $flds->[23+$i];
			}
		}
		elsif ( $flds->[$i] =~ /^Bene\s+Descrip/i &&
			$flds->[25+$i] =~ /HEALTHCHOICE/i ) 
		{
			if( $flds->[38+$i] =~ /Prov/ ) {
				$href->{'c_insurance_desc1'} = $flds->[39+$i];
			}
			if ( $flds->[57+$i] =~ /Phone/i )  {
				$href->{'c_contact_phone1'} = $flds->[58+$i];
			}
		}
	}

	if ($href->{'active_flag'} >= 0 ) {
		$href->{'patient_ssn'}   = $flds->[77] if $flds->[76] =~ /^SSN/i;
                $href->{'plan_type'}  = $flds->[133];
	}
}

1;





#============================================================
# Now it is in TPL package
#============================================================

package TPL;


#--------------------------------------------------------
# Start index of the repeated data chunks in each record
#--------------------------------------------------------
#our @chunks = (86,145,204,263,322,381,440,499,558,617);
#our @chunks = (93,152,211,270,329,388,447,506,565,624);
our @chunks = (108,167,226,285,344,403,462,521,580,639);



#----------------------------------------------------
# process(record, outfile, file_date, our_run_date)
#
#   Processing emdeon tpl data , and saving the
# result to another file.
#-----------------------------------------------------

sub process($$$$$)
{
        my $line = shift;
        my $OUTFH = shift;
        my $file_date = shift;
        my $our_run_date = shift;
        my $ctid = shift;

        $line =~ s/[^\x20-\x7e]//g;
        my @fields = split(/;/, $line);
        return if $fields[0] !~ /^M2/i;                         # record type

        my %h=();

        $h{'carrier_code'}            = $fields[1];
        $h{'input_ssn'}               = $fields[3];
        $h{'input_lname'}             = $fields[4];
        $h{'input_fname'}             = $fields[5];
        $h{'input_mname'}             = $fields[6];
        $h{'emdeon_matchkey'}         = $fields[17];
        $h{'cust_id'}                 = $ctid;
        $h{'our_run_date'}            = $our_run_date;
        $h{'file_date'}               = $file_date;


        ##################################################################################################

        # SUBSCRIBER NAME, DOB and SUBSCRIBER ID;
        $h{'subscriber_dob'} = ::date10($fields[65]);
        $h{'subscriber_id'}    = $fields[61];   # except '00017'
        $h{'subscriber_lname'} = $fields[62];
        $h{'subscriber_fname'} = $fields[63];
        $h{'subscriber_mname'} = $fields[64];

        # SUBSCRIBER ADDRESS INFO
        $h{'subscriber_addr1'} = $fields[67];
        $h{'subscriber_addr2'} = $fields[69];
        $h{'subscriber_city'}  = $fields[71];
        $h{'subscriber_state'} = $fields[72];
        $h{'subscriber_zip'}   = $fields[73];


        #################################################################################################
        # NOTE THAT BLOCKS OF DATA CONTAINING PATIENT NAME AND DOB INFORMATION ARE IN TWO DIFFERENT PLACES


        $h{'patient_lname'}          = $fields[47];
        $h{'patient_fname'}          = $fields[48];
        $h{'patient_dob'}   = ::date10($fields[49]);

        # WE OVERWRITTE THE ORIGINAL VALUES IF THE FIELDS BELOW ARE POPULATED
        if ( $fields[93] ne "") { $h{'patient_lname'} = $fields[93];}
        if ( $fields[94] ne "") { $h{'patient_fname'} = $fields[94];}
        if ( $fields[95] ne "") { $h{'patient_mname'} = $fields[95];}
        if ( $fields[96] ne "") { $h{'patient_dob'} = ::date10($fields[96]);}
        if ( $fields[97] ne "") { $h{'patient_gender'} = $fields[97];}

        # FOR GENDER: IF NOT POPULATED AND SUBSCRIBER = PATIENT THEN POPULATE WITH SUBSCRIBER FIELD
        if ( ($h{'patient_gender'} eq "")                    &&
             ($fields[96] eq $fields[65])                    &&
             ($h{'patient_lname'} eq $h{'subscriber_lname'}) &&
             ($h{'patient_fname'} eq $h{'subscriber_fname'})
           )
           { $h{'patient_gender'} = $fields[66] ;}


       # PATIENT ADDRESS INFO
        $h{'patient_addr1'} = $fields[98];
        $h{'patient_addr2'} = $fields[100];
        $h{'patient_city'}  = $fields[102];
        $h{'patient_state'} = $fields[103];
        $h{'patient_zip'}   = $fields[104];



        ##################################################################################################

        # TRUNCATE MIDDLE NAMES AND GENDER TO ONE BYTE
        $h{'input_mname'} =~ s/^\s*(\w).*$/$1/;
        $h{'subscriber_mname'} =~ s/^\s*(\w).*$/$1/;
        $h{'patient_mname'} =~ s/^\s*(\w).*$/$1/;
        $h{'patient_gender'} =~ s/^\s*(\w).*$/$1/;



        ##################################################################################################

        # CAPTURE THE search_pattern FIELD

        if ( $fields[699] =~ /SearchStats/i ) {$h{'search_pattern'} = uc($fields[700]) ;}
        else                                  {$h{'search_pattern'} = "";}
        $h{'search_pattern'} = substr($h{'search_pattern'}, 0, 30) if length($h{'search_pattern'}) > 30;


        ##################################################################################################

        # INITIALIZATION OF FLAGS

        $h{'suppress_flag'}    = 0;
        $h{'active_flag'}      = -1 ;   # initial value

        # initialize to value D = Medicaid OR C = Commercial;
        $h{'medlytix_ins_type'} = ($com_ccode{$h{'carrier_code'}})? "C" : "D";

        if ( $fields[22] !~ /^\s*$/) {  #err msg
                #-- $fields[20] is the NPI
                ::watch_error($fields[20], $h{'carrier_code'}, $fields[22]);
                return;
        }



        ###################################################################################################
        # CALL THE HANDLERS

        hdl_tpl(\%h, \@fields);


        ####################################################################################
        # EXCLUDE IF NOT FOUND ACTIVE 
        # EXCLUDE IF NON-MEDICAL
        # EXCLUDE IF AUTO INSURANCE

        return if $h{'active_flag'} < 1;
        return if $h{'plan_type'} =~ /(VISION|VIEW|DENTAL|DNTL|PHARM|RX|Chiropractic)/i;
        return if $fields[51] =~ /^(AFLAC|ANICO|NTWDE|GEICO)/i ;


        ####################################################################################
        # TREATMENT OF PLAN TYPE AND TRUNCATION
        $h{'plan_type'} =~ s/\s*\-\s*\-\s*$/ -  / if $h{'plan_type'} =~ /\-\s*\-/;
        $h{'plan_type'} =~ s/^\s*\-\s*// if $h{'plan_type'} =~ /^\s*\-/;
        $h{'plan_type'} =~ s/\s*\-\s*$// if $h{'plan_type'} =~ /\-\s*$/;
        $h{'plan_type'} = substr($h{'plan_type'}, 0, 118) if length($h{'plan_type'}) >119;


        ####################################################################################
        # TREATMENT OF SSN
        # FOR SOCIAL: IF SUBSCRIBER = PATIENT THEN POPULATE subscriber_ssn WITH patient_ssn

        if (! ::ssn_ok($h{'patient_ssn'}) ) {
                $h{'patient_ssn'} = '';
        }

        # extract specially from Medicare subscriber ID
        if ( ( $h{'patient_ssn'} eq "") && ($h{'input_ssn'} eq substr($h{'subscriber_id'}, 0, 9) ) ) {

                 $h{'patient_ssn'} = substr($h{'subscriber_id'}, 0, 9);
        }

        if (
             ($fields[96] eq $fields[65])                    &&
             ($h{'patient_lname'} eq $h{'subscriber_lname'}) &&
             ($h{'patient_fname'} eq $h{'subscriber_fname'})
           )
           { $h{'subscriber_ssn'} = $h{'patient_ssn'} ;}


        ####################################################################################
        # ASSIGNMENT OF insurance_desc NAME BASED ON HASH AND emdeon_ins_type BASED ON HASH

        $h{'emdeon_ins_type'} = "T";
        if ( $fields[50] eq $fields[51]) {$h{'insurance_desc'} = $fields[51];}
        else                             {$h{'insurance_desc'} = $fields[50] . " " . $fields[51];}



        ####################################################################################
        # CORRECTION OF SOME VALUES FOR GA MEDICAID

        if ( $h{'carrier_code'} eq "00006" ) {
             $h{'insurance_desc'} = "GEORGIA MEDICAID / PEACHCARE";
             $h{'emdeon_ins_type'} = "D";
        }


        if ( $h{'carrier_code'} eq "00006" || $h{'carrier_code'} eq "00242" ) {
           $h{'medlytix_ins_type'} = "D";
        }
        else {
           $h{'medlytix_ins_type'} = "C";
        }




        ####################################################################################
        # SUPPRESSION OF SOME ACCOUNTS (THESE WILL BE LOADED BUT NOT RETURNED TO CLIENT
        # FIRST WHEN NOT ACTIVE
        # SECOND SUPPRESS MEDICAID FOUND FOR HMA 

        $h{'suppress_flag'} +=1 if $h{'active_flag'} == 0;

                
        if ( ($h{'cust_id'} eq '49') &&  $h{'medlytix_ins_type'} eq "D" )
        {
                $h{'suppress_flag'} +=1000;
        }




        #------------------------------------------
        # Normalize date format to mm/dd/yyyy
        #------------------------------------------
        $h{'effective_date'}            = ::date10($h{'effective_date'});
        $h{'effective_date_end'}        = ::date10($h{'effective_date_end'});
        $h{'effective_date2'}           = ::date10($h{'effective_date2'});
        $h{'effective_date_end2'}       = ::date10($h{'effective_date_end2'});
        $h{'effective_date3'}           = ::date10($h{'effective_date3'});
        $h{'effective_date_end3'}       = ::date10($h{'effective_date_end3'});
        $h{'c_effective_date1'}         = ::date10($h{'c_effective_date1'});
        $h{'c_effective_date_end1'}     = ::date10($h{'c_effective_date_end1'});
        $h{'c_effective_date2'}         = ::date10($h{'c_effective_date2'});
        $h{'c_effective_date_end2'}     = ::date10($h{'c_effective_date_end2'});


                #----------------
                # @fdname determines the order of output
                #----------------
        ::print_record(\%h, \@::fdname, $OUTFH);
}



#---------------------------------------------------------------------
# Handler for "TPL" data.
#
#---------------------------------------------------------------------

sub hdl_tpl{
	my $href = shift;
	my $flds = shift;

	foreach my $i (108) {
		if ( $flds->[$i] =~ /Inactv/i ) {
			$href->{'active_flag'} = 0;
		}
		elsif($flds->[$i] =~ /Actv\s+Cvg/i &&
			$flds->[3+$i] =~ /Health\s+Bene\s+Plan\s+Cvg/i )
		{
			$href->{'active_flag'} = 1;
		}

	}

	if ($href->{'active_flag'} >= 0 ) {
		$href->{'patient_ssn'}   = $flds->[79] if $flds->[78] =~ /^SSN/i;
		
		if    ($flds->[78] =~ /^Grp #/i)  {$href->{'group_number'}     = $flds->[79];}
		elsif ($flds->[80] =~ /^Grp #/i)  {$href->{'group_number'}     = $flds->[81];}
		elsif ($flds->[123] =~ /^Grp #/i) {$href->{'group_number'}     = $flds->[124];}
		elsif ($flds->[125] =~ /^Grp #/i) {$href->{'group_number'}     = $flds->[126];}
		
		if    ($flds->[78] =~ /^Plan #/i)  {$href->{'plan_number'}     = $flds->[79];}
		elsif ($flds->[80] =~ /^Plan #/i)  {$href->{'plan_number'}     = $flds->[81];}
		elsif ($flds->[123] =~ /^Plan #/i) {$href->{'plan_number'}     = $flds->[124];}
		elsif ($flds->[125] =~ /^Plan #/i) {$href->{'plan_number'}     = $flds->[126];}
		
		$href->{'plan_type'}            = $flds->[116];

		if ($flds->[88] =~ /^Elig/i ) {
			my($s, $e) = split(/-/, $flds->[89]);
			if ($s =~ /\d/) {$href->{'effective_date'}     = $s;}
			if ($e ne  "") {
				my($l, $p , $q) = split(/\//, $e);
				if ($q !~ /9999/) {$href->{'effective_date_end'} = $e;}
			}
		}
	}
}

1;



#============================================================
# Now it is in MEDICARE package
#============================================================

package MEDICARE;

#-------------------------------------------------------------
# process(record, outfile, file_date, our_run_date)
#
#  Processing emdeon medicare data file, saving the 
# result in the output file.
#------------------------------------------------------------

sub process($$$$$) 
{
	my $line = shift;
	my $OUTFH = shift;
	my $file_date = shift;
	my $our_run_date = shift;
	my $ctid = shift;


	$line =~ s/[^\x20-\x7e]//g;
	my @fields = split(/;/, $line);
	return if $fields[0] !~ /^00024$/;			# carrier code		
	return if $fields[2] !~ /^\d{9}$/;			# ssn
	
	my %h=();									# clean old record data
	
	$h{'carrier_code'}            = $fields[0];
	$h{'patient_ssn'}          = $fields[1]; # It's really a ID, not ssn
	$h{'input_ssn'}     	      = $fields[2];
	$h{'cust_id'}                 = $ctid;
	$h{'input_lname'}             = $fields[3];
	$h{'input_fname'}             = $fields[4];
	$h{'input_mname'}             = $fields[5];
	$h{'emdeon_matchkey'}         = $fields[11];
	
		#-------------------------------------------------------------------------
		# There is a suffix(like 'A', 'B','C1','D', etc.) at the end of
		# 'patient_ssn'. The meaning is defined here:
		# http://www.slideshare.net/karna.indian/medicare-hic-number-suffixes-presentation
		#-------------------------------------------------------------------------
	$h{'input_mname'} =~ s/^\s*(\w).*$/$1/;
	$h{'patient_ssn'}  =~ s/^[A-Z]+//i;   	# remove any Prefix: [A-Z]{1,3} 
	$h{'patient_ssn'}  =~ s/[A-Z]+\d*//i;   	# remove any Suffix: [A-Z][1-9]  

	if( $fields[27] =~/\d+/){					# Med A
		$h{'effective_date'} = ::date10($fields[27]);
	}
	if( $fields[28] =~/\d+/){
		$h{'effective_date_end'} = ::date10($fields[28]);
	}

	if( $fields[31] =~ /\d+/){					# Med B
		$h{'medicareB_date'} = ::date10($fields[31]);
	}
	if( $fields[32] =~ /\d+/){
		$h{'medicareB_date_end'} = ::date10($fields[32]);
	}
	
	$h{'patient_lname'}	= $fields[33]; 
	$h{'patient_fname'}	= $fields[34]; 
	$h{'patient_mname'}	= $fields[35];
        $h{'patient_mname'} =~ s/^\s*(\w).*$/$1/;

        $h{'patient_gender'} = '';
        $h{'patient_addr1'} = '';
        $h{'patient_addr2'} = '';
        $h{'patient_city'}  = '';
        $h{'patient_state'} = '';
        $h{'patient_zip'}   = '';

	
	if($fields[37] =~ /\d+/) {
		$h{'patient_dob'} = ::date10($fields[37]);	
	}
	
	$h{'subscriber_id'} = $fields[38];			# medicare ID
	$h{'error_message'} = $fields[39];

	$h{'subscriber_lname'} = $h{'patient_lname'};
	$h{'subscriber_fname'} = $h{'patient_fname'};
	$h{'subscriber_mname'} = $h{'returned_mname'};
	$h{'subscriber_dob'}   = $h{'patient_dob'};
	$h{'subscriber_addr1'} = '';
	$h{'subscriber_addr2'} = '';
	$h{'subscriber_city'}  = '';
	$h{'subscriber_state'} = '';
	$h{'subscriber_zip'}   = '';
	$h{'suppress_flag'}  = 0;
	
	if( $h{'error_message'} =~ /^Found/i ) {
		$h{'active_flag'} = 1;
	}else { return; }				# discard the data
	
	$h{'medicare_contract1'} = $fields[62];
	$h{'medicare_contract2'} = $fields[67];

	if($fields[64] =~ /\d+/) {
		$h{'start_contract_date1'} = ::date10($fields[64]);	
	}
	if($fields[65] =~ /\d+/) {
		$h{'end_contract_date1'}   = ::date10($fields[65]);	
	}
	if($fields[69] =~ /\d+/) {
		$h{'start_contract_date2'} = ::date10($fields[69]);	
	}
	if($fields[70] =~ /\d+/) {
		$h{'end_contract_date2'}   = ::date10($fields[70]);	
	}

	if (! ::ssn_ok($h{'patient_ssn'}) ) {
		$h{'patient_ssn'} = '';
	}
        $h{'subscriber_ssn'} = $h{'patient_ssn'};

	$h{'search_pattern'} = "";
	$h{'emdeon_ins_type'} = "M";
        $h{'medlytix_ins_type'} = "M";

	$h{'insurance_desc'}  = 'MEDICARE A';
	$h{'plan_number'}     = '';
	$h{'network_id'}      = '';
	$h{'group_number'}    = '';
	$h{'insurance_addr'}  = '';
	$h{'insurance_city'}  = '';
	$h{'insurance_state'} = '';
	$h{'insurance_zip'}   = '';
	$h{'plan_type'}       = '';

	$h{'file_date'}     = $file_date;
	$h{'our_run_date'}  = $our_run_date;
	$h{'suppress_flag'} += 1 if $h{'active_flag'} == 0;
	
		#---------------------------------------------
		# Shared table fields substitution
		#---------------------------------------------
	$h{'insurance_desc2'}  = "MEDICARE B"  if $h{'medicareB_date'} =~ /\d/; 
	$h{'subscriber_id2'}        = undef;
	$h{'effective_date2'}       = $h{'medicareB_date'};
	$h{'effective_date_end2'}   = $h{'medicareB_date_end'};
	$h{'insurance_contact_phone2'}  = undef;

	$h{'c_insurance_desc1'}     = $h{'medicare_contract1'};
	$h{'c_subscriber_id1'}      = undef;
	$h{'c_effective_date1'}     = $h{'start_contract_date1'};
	$h{'c_effective_date_end1'} = $h{'end_contract_date1'};
	$h{'c_contact_phone1'}      = undef;

	$h{'c_insurance_desc2'}     = $h{'medicare_contract2'};
	$h{'c_subscriber_id2'}      = undef;
	$h{'c_effective_date2'}     = $h{'start_contract_date2'};
	$h{'c_effective_date_end2'} = $h{'end_contract_date2'};
	$h{'c_contact_phone2'}      = undef;

		#---------------------------------------------
		# The printing order is defined in @fdname
		#---------------------------------------------
	::print_record(\%h, \@::fdname, $OUTFH);

}	


1;



#============================================================
# Now it is in MEDICARE_FAST package
#============================================================

package MEDICARE_FAST;

#-------------------------------------------------------------
# process(record, outfile, file_date, our_run_date)
#
#  Processing emdeon medicare data file, saving the 
# result in the output file.
#------------------------------------------------------------

sub process($$$$$) 
{
	my $line = shift;
	my $OUTFH = shift;
	my $file_date = shift;
	my $our_run_date = shift;
	my $ctid = shift;

	$line =~ s/[^\x20-\x7e]//g;
	my @fields = split(/;/, $line);
	return if $fields[0] !~ /^MEDICARE/i;		# carrier code		
	
	my %h=();									# clean old record data
	
	$h{'carrier_code'}            = "00024";
	$h{'patient_ssn'}          = $fields[3]; # It's really a ID, not ssn
	$h{'input_ssn'}     	     = '';
	$h{'cust_id'}                 = $ctid;
	$h{'input_lname'}             = '';
	$h{'input_fname'}             = '';
	$h{'input_mname'}             = '';
	$h{'emdeon_matchkey'}         = $fields[1];
	
		#-------------------------------------------------------------------------
		# There is a suffix(like 'A', 'B','C1','D', etc.) at the end of
		# 'patient_ssn'. The meaning is defined here:
		# http://www.slideshare.net/karna.indian/medicare-hic-number-suffixes-presentation
		#-------------------------------------------------------------------------

	$h{'patient_ssn'}  =~ s/^[A-Z]+//i;   	# remove any Prefix: [A-Z]{1,3} 
	$h{'patient_ssn'}  =~ s/[A-Z]+\d*//i;   # remove any Suffix: [A-Z][1-9]  


    # Med A 
	if (($fields[13] =~ /\d+/) && ($fields[13] =~ /-/)) {
		my @flds= split(/-/, $fields[13]);
		$h{'effective_date'} = ::date10_conv($flds[0]);
		$h{'effective_date_end'} = ::date10_conv($flds[1]);
	} else {
		$h{'effective_date'} = ::date10_conv($fields[13]);
		$h{'effective_date_end'} = '';
	}
    
    # Med B
	if (($fields[15] =~ /\d+/) && ($fields[15] =~ /-/)) {					
		my @flds= split(/-/, $fields[15]);
		$h{'medicareB_date'} = ::date10_conv($flds[0]);
		$h{'medicareB_date_end'} = ::date10_conv($flds[1]);
	} else {
		$h{'medicareB_date'} = ::date10_conv($fields[15]);
		$h{'medicareB_date_end'} = '';
	}
	
	$h{'patient_lname'}	= $fields[4]; 
	$h{'patient_fname'}	= $fields[5]; 
	$h{'patient_mname'}	= $fields[6];
        $h{'patient_mname'} =~ s/^\s*(\w).*$/$1/;

        $h{'patient_gender'}= '';
        $h{'patient_addr1'} = '';
        $h{'patient_addr2'} = '';
        $h{'patient_city'}  = '';
        $h{'patient_state'} = '';
        $h{'patient_zip'}   = '';

	
	if($fields[7] =~ /\d+/) {
		$h{'patient_dob'} = ::date10_conv($fields[7]);	
	}
	
	$h{'subscriber_id'} = $fields[3];			# medicare ID
	$h{'error_message'} = $fields[12];

	$h{'subscriber_lname'} = $h{'patient_lname'};
	$h{'subscriber_fname'} = $h{'patient_fname'};
	$h{'subscriber_mname'} = $h{'returned_mname'};
	$h{'subscriber_dob'}   = $h{'patient_dob'};
	$h{'subscriber_addr1'} = '';
	$h{'subscriber_addr2'} = '';
	$h{'subscriber_city'}  = $fields[9];
	$h{'subscriber_state'} = $fields[10];
	$h{'subscriber_zip'}   = $fields[11];
	$h{'suppress_flag'}  = 0;
	
	if( $h{'error_message'} =~ /^YES/i ) {
		$h{'active_flag'} = 1;
	}else { return; }				# discard the data
	
	if ($fields[19] eq "YES") {
		my @flds =  split(/ /, $fields[21]);
		$h{'medicare_contract1'} = $flds[0];
	}
	$h{'medicare_contract2'} = '';

	if($fields[20] =~ /\d+/) {
		$h{'start_contract_date1'} = ::date10_conv($fields[20]);	
	}
	$h{'end_contract_date1'}   = '';	
	$h{'start_contract_date2'} = '';	
	$h{'end_contract_date2'}   = '';	

	if (! ::ssn_ok($h{'patient_ssn'}) ) {
		$h{'patient_ssn'} = '';
	}
        $h{'subscriber_ssn'} = $h{'patient_ssn'};

	$h{'search_pattern'} = "";
	$h{'emdeon_ins_type'} = "M";
        $h{'medlytix_ins_type'} = "M";
	$h{'insurance_desc'}  = 'MEDICARE A';
	$h{'plan_number'}     = '';
	$h{'network_id'}      = '';
	$h{'group_number'}    = '';
	$h{'insurance_addr'}  = '';
	$h{'insurance_city'}  = '';
	$h{'insurance_state'} = '';
	$h{'insurance_zip'}   = '';
	$h{'plan_type'}       = '';

	$h{'file_date'}     = $file_date;
	$h{'our_run_date'}  = $our_run_date;
	$h{'suppress_flag'} += 1 if $h{'active_flag'} == 0;
	
		#---------------------------------------------
		# Shared table fields substitution
		#---------------------------------------------
	$h{'insurance_desc2'}  = "MEDICARE B"  if $h{'medicareB_date'} =~ /\d/; 
	$h{'subscriber_id2'}        = undef;
	$h{'effective_date2'}       = $h{'medicareB_date'};
	$h{'effective_date_end2'}   = $h{'medicareB_date_end'};
	$h{'insurance_contact_phone2'}  = undef;

	$h{'c_insurance_desc1'}     = $h{'medicare_contract1'};
	$h{'c_subscriber_id1'}      = undef;
	$h{'c_effective_date1'}     = $h{'start_contract_date1'};
	$h{'c_effective_date_end1'} = $h{'end_contract_date1'};
	$h{'c_contact_phone1'}      = undef;

	$h{'c_insurance_desc2'}     = $h{'medicare_contract2'};
	$h{'c_subscriber_id2'}      = undef;
	$h{'c_effective_date2'}     = $h{'start_contract_date2'};
	$h{'c_effective_date_end2'} = $h{'end_contract_date2'};
	$h{'c_contact_phone2'}      = undef;

		#---------------------------------------------
		# The printing order is defined in @fdname
		#---------------------------------------------
	::print_record(\%h, \@::fdname, $OUTFH);

}	

1;


#==================================================
#  Back to package main
#==================================================

package main;

#-----------------------------------------------------------
# print_record(hash_ref, array_ref, Filehandle)
#
#   Print hash items specified in 'array_ref' to the 
#   Filehandle.
#-----------------------------------------------------------
sub print_record(\%\@*)
{
	my $hr = shift;
	my $aref = shift;
	my $FH = shift;						# output File handler

	my $i=0;
	foreach my $fld ( @$aref ) {
		if( exists $hr->{$fld} ) {
			$hr->{$fld} =~ s/\|//g;		# remove any PIPE
			$hr->{$fld} =~ s/\s*$//;	# remove any trail space 
			print $FH $hr->{$fld};
		}

		if($i != $#$aref ) {
			print $FH "|";
		} else {
			print $FH "\n";
		}
		$i ++;
	}
}


#-----------------------------------------------------------
# date6to8( date ) 
#	Convert 'mmddyy' to 'mm/dd/yy' format.
#-----------------------------------------------------------
sub date6to8($) 
{
	my $d = shift;
	if ( $d !~ /^\d{6}$/ ){ return $d; }
	$d =~ m|^(\d\d)(\d\d)(\d\d)|;
	return "$1/$2/$3";
}

#-----------------------------------------------------------
# date6to10(date) 
#	Convert 'mmddyy' to 'mm/dd/yyyy' format.
#
#  yearcutoff = 1930
#-----------------------------------------------------------
sub date6to10($) 
{
	my $d = shift;
	if ( $d !~ /^\d{6}\b/ ){ return $d; }
	$d =~ m|^(\d\d)(\d\d)(\d\d)|;
	my $m = $1;
	my $d = $2;
	my $y =  ($3<30)? $3 + 2000 : $3 + 1900 ; 
	return "$m/$d/$y";
}

#-----------------------------------------------------------
# date8to10(date)
#	Convert '20yymmdd' to 'mm/dd/yyyy' format.
#	Convert 'mmddyyyy' to 'mm/dd/yyyy' format.
#	Convert 'mm/dd/yy' to 'mm/dd/yyyy' format.
#-----------------------------------------------------------
sub date8to10($)
{
	my $d = shift;
	if ( $d =~ /^\d{8}\b/ ){ 
		if( $d =~ /^20/ ) {
			$d =~ /^(\d\d\d\d)(\d\d)(\d\d)/;
			return "$2/$3/$1";
		} else {
			$d =~ /^(\d\d)(\d\d)(\d\d\d\d)/;
			return "$1/$2/$3";
		}
	}
	elsif ( $d =~ m|^(\d\d/\d\d/)(\d\d)\b| ) {
		my $y;
		if($2 < 30 ) { $y = 2000 + $2;}
		else { $y = 1900 + $2; }
		return "$1$y";
	}
	return $d;
}

#-----------------------------------------------------------
# date10(date)
#  Convert input date to 'mm/dd/yyyy' format.
#-----------------------------------------------------------
sub date10($)
{
	my $d = shift;
	my $ret;
	if( $d =~ /^(\d{8})\b/){					# mmddyyyy
		$ret = date8to10($1);
	} elsif( $d =~ /^(\d{6})\b/){				# mmddyy
		$ret = date6to10($1);
	} elsif ( $d =~ m|^(\d\d/\d\d/)(\d\d)\b| ){	# mm/dd/yy
		my $y;
		if($2 < 30 ) { $y = 2000 + $2;}
		else { $y = 1900 + $2; }
		return $ret = "$1$y";
	} else {
		$ret = $d;
	}

	if ( $ret =~ m|(\d\d)/(\d\d)/(\d\d\d\d)| ) {
		my $mon = $1;
		my $day = $2;
		my $yr  = $3;

		if( $day <= 0 || $day > 31 || $mon <= 0 || 
			$mon > 12 || $yr <= 0  || $yr > 2200 ) 
		{ 
			$ret = '';
		}
	}

	return $ret;
}


#-----------------------------------------------------------
# datetime19(datetime)
#  Convert MMDDYY_HHMMSS to 'mm/dd/yyyy hh:mm:ss' format.
#-----------------------------------------------------------
sub datetime19($)
{
        my $d = shift;
        my $ret;
        if( $d =~ /^(\d{6})_(\d\d)(\d\d)(\d\d)\b/) {                     # mmddyy_hhmmss
                $ret = date6to10($1) . " " . $2 . ":" . $3 . ":" . $4;
        } else {
                $ret = '';
        }

        return $ret;
}


#----------------------------------------------------------
# date10_conv(date)
# Convert input date YYYYMMDD to 'mm/dd/yyyy' format	
#----------------------------------------------------------
sub date10_conv($)
{
    my $d = shift;
    my $yyyy = substr($d, 0, 4);
    my $mm = substr($d, 4, 2);
    my $dd = substr($d, 6, 2); 
    my $dt;
 
    if ( $yyyy ne "") 
      {$dt = $mm . '/' . $dd . '/' . $yyyy;} 
    else
      { $dt = '';}
 
    return $dt;
}



#-----------------------------------------------------------
# ssn_ok(number)
#  Judge if an number is a valid SSN.
#
#Output:
#    1  --> yes
#    0  --> no
#-----------------------------------------------------------
sub ssn_ok($)
{
	my $ssn = shift;

	if( $ssn !~ /^\d{9}$/) { return 0; }

	foreach ('000000000', '111111111', '222222222',
			 '333333333', '444444444', '555555555',
			 '666666666', '777777777', '888888888',
			 '999999999', '123456789', '078051120') 
	{
		if ( $ssn =~ /$_/ ) { return 0; }
	}


	if(	substr($ssn,0,3) eq '000'  ||
		substr($ssn,3,2) eq '00'   ||
		substr($ssn,5,4) eq '0000' || 
		substr($ssn,0,3) eq '666'  || 
		substr($ssn,0,3) > 800 ) 
	{
		return 0;
	}

	return 1;
}

#-------------------------------------------------
# Generate file last modified date in local time.
#-------------------------------------------------
sub get_file_date ($)
{
        my $file = shift;
        my ($day,$mon,$year) = (localtime((stat($file))[9]))[3,4,5];
        $year += 1900;
        $mon++;

        return sprintf "%02d/%02d/%4d", $mon,$day,$year;
}

sub today()
{
	my ($day,$mon,$year) = (localtime)[3,4,5];
	$year += 1900;
	$mon++;

	return sprintf "%02d/%02d/%4d", $mon,$day,$year;
}


sub handle_quotes($){
	my $input = shift;
	my @bag=split(/\"/, $input);
	return undef if (@bag % 2) == 0;
	my $i=0;
	my $nl="";
	foreach my $part ( @bag ) {
		$part =~ s/;//g if $i % 2 ;
		$nl .= $part;
		$i++;
	}
	return $nl;
}


#------------------------------------------
# Using global var:  $err_monitor
#------------------------------------------
sub watch_error($$$) {
	my $NPI = shift;
	my $ccode = shift;
	my $msg = shift;

	my $errid ;
	if ($msg =~ /^\s*(RH0031|RH0046|RH0079|RH0231|RH0238|RH0923|RH0925|RH0085)/i ) {
		$errid = $1;
		if(exists $err_monitor->{$NPI}{$ccode}{$errid} ){
			$err_monitor->{$NPI}{$ccode}{$errid}{count} ++;
		} else {
			$err_monitor->{$NPI}{$ccode}{$errid}{count} =1;
			$err_monitor->{$NPI}{$ccode}{$errid}{fullmsg} =$msg;
		}
	}
}

sub report_error($) {
	my $custid= shift;
	my $err_cnt = scalar keys %$err_monitor;
	return if $err_cnt == 0;

	my $msgbody = "     --------- Cust ID : $custid ------------\n";
	$msgbody .= sprintf("%15s %10s %8s %s\n", "NPI  ","Carr. Code","Count", "   Err Message");
	foreach my $npi ( keys %$err_monitor ) {
		foreach my $ccode (sort {$a <=> $b}  keys %{$err_monitor->{$npi}} ) {
			foreach my $ei ( sort keys %{$err_monitor->{$npi}{$ccode}} ) {
				$msgbody .= sprintf("%15s %10s %8d  %s\n", $npi,$ccode,
					$err_monitor->{$npi}{$ccode}{$ei}{count},
					$err_monitor->{$npi}{$ccode}{$ei}{fullmsg});
			}
		}
	}
	$msgbody .="\n";

        my $msg = MIME::Lite->new(
                From    => 'payorintel_batch@medlytix.com',
                #To      => 'yi.yan@medlytix.com',	#keep for testing
                To      => 'support@medlytix.com',
                Subject => 'Error Msg Summary for emdeon return file',
                Data    => $msgbody
                );
        return if ! $msg;

        $msg->send('smtp', 'mailhost', Timeout => 600);

	$err_monitor = undef;
}


sub usage() {
	print " Usage:\n";
	print "\tconvert_emdeon.pl -b MMDDYY_HHMMSS_XXXXXX  infile  outfile\n";
	exit 1;
}



#==========================================
# Commands
#==========================================
our $opt_b;
getopts('b:');

if ( !$opt_b || @ARGV != 2 || $opt_b !~ /^\d*_\d*_\d*/ ) { usage();}

my $infile = $ARGV[0] ;
my $oufile = $ARGV[1] ;
open(INP, "<$infile") or die "Couldn't open $infile ";
open(OUP, ">$oufile") or die "Couldn't open $oufile ";

my $ctid = $opt_b;
$ctid =~ s/^\d*_\d*_// if $ctid =~ /\d*_\d*_/;
$ctid =~ s/^0*//;

$opt_b =~ s/_\d*$// if $opt_b =~ /_\d*/;
my $run_date  = datetime19($opt_b);
my $file_date = get_file_date($infile);

my $record;
while( $record = <INP> ) {

	$record = handle_quotes($record) if $record =~ /\"/;

	if( $record =~ /^Remediated\;/i) {	
		COM_MEDICAID::process("$record", \*OUP, $file_date, $run_date , $ctid); 
	}
	elsif($record =~ /^00024\;/) {
		MEDICARE::process("$record", \*OUP, $file_date, $run_date, $ctid ); 
	}
	elsif($record =~ /^M2\;/i) {
		TPL::process("$record", \*OUP, $file_date, $run_date , $ctid); 
	}
        elsif($record =~ /^MEDICARE\;/i) {
                MEDICARE_FAST::process("$record", \*OUP, $file_date, $run_date , $ctid);
        }
	else {
		$bad_record_num ++;
		;#print STDERR $record;
	}
}

report_error($ctid);

if(-t STDERR and $bad_record_num > 5) {
	print STDERR "Bad records total: $bad_record_num \n";
}


